home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / search.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  105KB  |  3,535 lines

  1. /* Breadth-first and depth-first routines for
  2.    searching multiple-inheritance lattice for GNU C++.
  3.    Copyright (C) 1987, 1989, 1992, 1993, 1995 Free Software Foundation, Inc.
  4.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.  */
  22.  
  23. /* High-level class interface. */
  24.  
  25. #include "config.h"
  26. #include "tree.h"
  27. #include <stdio.h>
  28. #include "cp-tree.h"
  29. #include "obstack.h"
  30. #include "flags.h"
  31. #include "rtl.h"
  32. #include "output.h"
  33.  
  34. #define obstack_chunk_alloc xmalloc
  35. #define obstack_chunk_free free
  36.  
  37. void init_search ();
  38. extern struct obstack *current_obstack;
  39. extern tree abort_fndecl;
  40.  
  41. #include "stack.h"
  42.  
  43. /* Obstack used for remembering decision points of breadth-first.  */
  44. static struct obstack search_obstack;
  45.  
  46. /* Methods for pushing and popping objects to and from obstacks.  */
  47. struct stack_level *
  48. push_stack_level (obstack, tp, size)
  49.      struct obstack *obstack;
  50.      char *tp;  /* Sony NewsOS 5.0 compiler doesn't like void * here.  */
  51.      int size;
  52. {
  53.   struct stack_level *stack;
  54.   obstack_grow (obstack, tp, size);
  55.   stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
  56.   obstack_finish (obstack);
  57.   stack->obstack = obstack;
  58.   stack->first = (tree *) obstack_base (obstack);
  59.   stack->limit = obstack_room (obstack) / sizeof (tree *);
  60.   return stack;
  61. }
  62.  
  63. struct stack_level *
  64. pop_stack_level (stack)
  65.      struct stack_level *stack;
  66. {
  67.   struct stack_level *tem = stack;
  68.   struct obstack *obstack = tem->obstack;
  69.   stack = tem->prev;
  70.   obstack_free (obstack, tem);
  71.   return stack;
  72. }
  73.  
  74. #define search_level stack_level
  75. static struct search_level *search_stack;
  76.  
  77. static tree lookup_field_1 ();
  78. static int lookup_fnfields_1 ();
  79. static void dfs_walk ();
  80. static int markedp ();
  81. static void dfs_unmark ();
  82. static void dfs_init_vbase_pointers ();
  83.  
  84. static tree vbase_types;
  85. static tree vbase_decl, vbase_decl_ptr;
  86. static tree vbase_decl_ptr_intermediate;
  87. static tree vbase_init_result;
  88.  
  89. /* Allocate a level of searching.  */
  90. static struct search_level *
  91. push_search_level (stack, obstack)
  92.      struct stack_level *stack;
  93.      struct obstack *obstack;
  94. {
  95.   struct search_level tem;
  96.  
  97.   tem.prev = stack;
  98.   return push_stack_level (obstack, (char *)&tem, sizeof (tem));
  99. }
  100.  
  101. /* Discard a level of search allocation.  */
  102. static struct search_level *
  103. pop_search_level (obstack)
  104.      struct stack_level *obstack;
  105. {
  106.   register struct search_level *stack = pop_stack_level (obstack);
  107.  
  108.   return stack;
  109. }
  110.  
  111. /* Search memoization.  */
  112. struct type_level
  113. {
  114.   struct stack_level base;
  115.  
  116.   /* First object allocated in obstack of entries.  */
  117.   char *entries;
  118.  
  119.   /* Number of types memoized in this context.  */
  120.   int len;
  121.  
  122.   /* Type being memoized; save this if we are saving
  123.      memoized contexts.  */
  124.   tree type;
  125. };
  126.  
  127. /* Obstack used for memoizing member and member function lookup.  */
  128.  
  129. static struct obstack type_obstack, type_obstack_entries;
  130. static struct type_level *type_stack;
  131. static tree _vptr_name;
  132.  
  133. /* Make things that look like tree nodes, but allocate them
  134.    on type_obstack_entries.  */
  135. static int my_tree_node_counter;
  136. static tree my_tree_cons (), my_build_string ();
  137.  
  138. extern int flag_memoize_lookups, flag_save_memoized_contexts;
  139.  
  140. /* Variables for gathering statistics.  */
  141. static int my_memoized_entry_counter;
  142. static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
  143. static int memoized_fields_searched[2];
  144. static int n_fields_searched;
  145. static int n_calls_lookup_field, n_calls_lookup_field_1;
  146. static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
  147. static int n_calls_get_base_type;
  148. static int n_outer_fields_searched;
  149. static int n_contexts_saved;
  150.  
  151. /* Local variables to help save memoization contexts.  */
  152. static tree prev_type_memoized;
  153. static struct type_level *prev_type_stack;
  154.  
  155. /* This list is used by push_class_decls to know what decls need to
  156.    be pushed into class scope.  */
  157. static tree closed_envelopes = NULL_TREE;
  158.  
  159. /* Allocate a level of type memoization context.  */
  160. static struct type_level *
  161. push_type_level (stack, obstack)
  162.      struct stack_level *stack;
  163.      struct obstack *obstack;
  164. {
  165.   struct type_level tem;
  166.  
  167.   tem.base.prev = stack;
  168.  
  169.   obstack_finish (&type_obstack_entries);
  170.   tem.entries = (char *) obstack_base (&type_obstack_entries);
  171.   tem.len = 0;
  172.   tem.type = NULL_TREE;
  173.  
  174.   return (struct type_level *)push_stack_level (obstack, (char *)&tem, sizeof (tem));
  175. }
  176.  
  177. /* Discard a level of type memoization context.  */
  178.  
  179. static struct type_level *
  180. pop_type_level (stack)
  181.      struct type_level *stack;
  182. {
  183.   obstack_free (&type_obstack_entries, stack->entries);
  184.   return (struct type_level *)pop_stack_level ((struct stack_level *)stack);
  185. }
  186.  
  187. /* Make something that looks like a TREE_LIST, but
  188.    do it on the type_obstack_entries obstack.  */
  189. static tree
  190. my_tree_cons (purpose, value, chain)
  191.      tree purpose, value, chain;
  192. {
  193.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
  194.   ++my_tree_node_counter;
  195.   TREE_TYPE (p) = NULL_TREE;
  196.   ((HOST_WIDE_INT *)p)[3] = 0;
  197.   TREE_SET_CODE (p, TREE_LIST);
  198.   TREE_PURPOSE (p) = purpose;
  199.   TREE_VALUE (p) = value;
  200.   TREE_CHAIN (p) = chain;
  201.   return p;
  202. }
  203.  
  204. static tree
  205. my_build_string (str)
  206.      char *str;
  207. {
  208.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
  209.   ++my_tree_node_counter;
  210.   TREE_TYPE (p) = 0;
  211.   ((int *)p)[3] = 0;
  212.   TREE_SET_CODE (p, STRING_CST);
  213.   TREE_STRING_POINTER (p) = str;
  214.   TREE_STRING_LENGTH (p) = strlen (str);
  215.   return p;
  216. }
  217.  
  218. /* Memoizing machinery to make searches for multiple inheritance
  219.    reasonably efficient.  */
  220. #define MEMOIZE_HASHSIZE 8
  221. typedef struct memoized_entry
  222. {
  223.   struct memoized_entry *chain;
  224.   int uid;
  225.   tree data_members[MEMOIZE_HASHSIZE];
  226.   tree function_members[MEMOIZE_HASHSIZE];
  227. } *ME;
  228.  
  229. #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
  230. #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
  231. #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
  232. #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
  233. /* The following is probably a lousy hash function.  */
  234. #define MEMOIZED_HASH_FN(NODE) (((long)(NODE)>>4)&(MEMOIZE_HASHSIZE - 1))
  235.  
  236. static struct memoized_entry *
  237. my_new_memoized_entry (chain)
  238.      struct memoized_entry *chain;
  239. {
  240.   struct memoized_entry *p =
  241.     (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
  242.                         sizeof (struct memoized_entry));
  243.   bzero ((char *) p, sizeof (struct memoized_entry));
  244.   MEMOIZED_CHAIN (p) = chain;
  245.   MEMOIZED_UID (p) = ++my_memoized_entry_counter;
  246.   return p;
  247. }
  248.  
  249. /* Make an entry in the memoized table for type TYPE
  250.    that the entry for NAME is FIELD.  */
  251.  
  252. tree
  253. make_memoized_table_entry (type, name, function_p)
  254.      tree type, name;
  255.      int function_p;
  256. {
  257.   int index = MEMOIZED_HASH_FN (name);
  258.   tree entry, *prev_entry;
  259.  
  260.   memoized_adds[function_p] += 1;
  261.   if (CLASSTYPE_MTABLE_ENTRY (type) == 0)
  262.     {
  263.       obstack_ptr_grow (&type_obstack, type);
  264.       obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
  265.       CLASSTYPE_MTABLE_ENTRY (type) = (char *)my_new_memoized_entry ((struct memoized_entry *)0);
  266.       type_stack->len++;
  267.       if (type_stack->len * 2 >= type_stack->base.limit)
  268.     my_friendly_abort (88);
  269.     }
  270.   if (function_p)
  271.     prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  272.   else
  273.     prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  274.  
  275.   entry = my_tree_cons (name, NULL_TREE, *prev_entry);
  276.   *prev_entry = entry;
  277.  
  278.   /* Don't know the error message to give yet.  */
  279.   TREE_TYPE (entry) = error_mark_node;
  280.  
  281.   return entry;
  282. }
  283.  
  284. /* When a new function or class context is entered, we build
  285.    a table of types which have been searched for members.
  286.    The table is an array (obstack) of types.  When a type is
  287.    entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
  288.    field is set to point to a new record, of type struct memoized_entry.
  289.  
  290.    A non-NULL TREE_TYPE of the entry contains an access control error message.
  291.  
  292.    The slots for the data members are arrays of tree nodes.
  293.    These tree nodes are lists, with the TREE_PURPOSE
  294.    of this list the known member name, and the TREE_VALUE
  295.    as the FIELD_DECL for the member.
  296.  
  297.    For member functions, the TREE_PURPOSE is again the
  298.    name of the member functions for that class,
  299.    and the TREE_VALUE of the list is a pairs
  300.    whose TREE_PURPOSE is a member functions of this name,
  301.    and whose TREE_VALUE is a list of known argument lists this
  302.    member function has been called with.  The TREE_TYPE of the pair,
  303.    if non-NULL, is an error message to print.  */
  304.  
  305. /* Tell search machinery that we are entering a new context, and
  306.    to update tables appropriately.
  307.  
  308.    TYPE is the type of the context we are entering, which can
  309.    be NULL_TREE if we are not in a class's scope.
  310.  
  311.    USE_OLD, if nonzero tries to use previous context.  */
  312. void
  313. push_memoized_context (type, use_old)
  314.      tree type;
  315.      int use_old;
  316. {
  317.   int len;
  318.   tree *tem;
  319.  
  320.   if (prev_type_stack)
  321.     {
  322.       if (use_old && prev_type_memoized == type)
  323.     {
  324. #ifdef GATHER_STATISTICS
  325.       n_contexts_saved++;
  326. #endif
  327.       type_stack = prev_type_stack;
  328.       prev_type_stack = 0;
  329.  
  330.       tem = &type_stack->base.first[0];
  331.       len = type_stack->len;
  332.       while (len--)
  333.         CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = (char *)tem[len*2+1];
  334.       return;
  335.     }
  336.       /* Otherwise, need to pop old stack here.  */
  337.       type_stack = pop_type_level (prev_type_stack);
  338.       prev_type_memoized = 0;
  339.       prev_type_stack = 0;
  340.     }
  341.  
  342.   type_stack = push_type_level ((struct stack_level *)type_stack,
  343.                 &type_obstack);
  344.   type_stack->type = type;
  345. }
  346.  
  347. /* Tell search machinery that we have left a context.
  348.    We do not currently save these contexts for later use.
  349.    If we wanted to, we could not use pop_search_level, since
  350.    poping that level allows the data we have collected to
  351.    be clobbered; a stack of obstacks would be needed.  */
  352. void
  353. pop_memoized_context (use_old)
  354.      int use_old;
  355. {
  356.   int len;
  357.   tree *tem = &type_stack->base.first[0];
  358.  
  359.   if (! flag_save_memoized_contexts)
  360.     use_old = 0;
  361.   else if (use_old)
  362.     {
  363.       len = type_stack->len;
  364.       while (len--)
  365.     tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
  366.  
  367.       prev_type_stack = type_stack;
  368.       prev_type_memoized = type_stack->type;
  369.     }
  370.  
  371.   if (flag_memoize_lookups)
  372.     {
  373.       len = type_stack->len;
  374.       while (len--)
  375.     CLASSTYPE_MTABLE_ENTRY (tem[len*2])
  376.       = (char *)MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
  377.     }
  378.   if (! use_old)
  379.     type_stack = pop_type_level (type_stack);
  380.   else
  381.     type_stack = (struct type_level *)type_stack->base.prev;
  382. }
  383.  
  384. /* This is the newer recursive depth first search routine. */
  385. #if 0                /* unused */
  386. /* Return non-zero if PARENT is directly derived from TYPE.  By directly
  387.    we mean it's only one step up the inheritance lattice.  We check this
  388.    by walking horizontally across the types that TYPE directly inherits
  389.    from, to see if PARENT is among them.  This is used by get_binfo and
  390.    by compute_access.  */
  391. static int
  392. immediately_derived (parent, type)
  393.      tree parent, type;
  394. {
  395.   if (TYPE_BINFO (type))
  396.     {
  397.       tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
  398.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  399.  
  400.       for (i = 0; i < n_baselinks; i++)
  401.     {
  402.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  403.  
  404.       if (parent == BINFO_TYPE (base_binfo))
  405.         return 1;
  406.     }
  407.     }
  408.   return 0;
  409. }
  410. #endif
  411.  
  412. /* Check whether the type given in BINFO is derived from PARENT.  If
  413.    it isn't, return 0.  If it is, but the derivation is MI-ambiguous
  414.    AND protect != 0, emit an error message and return error_mark_node.
  415.  
  416.    Otherwise, if TYPE is derived from PARENT, return the actual base
  417.    information, unless a one of the protection violations below
  418.    occurs, in which case emit an error message and return error_mark_node.
  419.  
  420.    If PROTECT is 1, then check if access to a public field of PARENT
  421.    would be private.  Also check for ambiguity.  */
  422.  
  423. tree
  424. get_binfo (parent, binfo, protect)
  425.      register tree parent, binfo;
  426.      int protect;
  427. {
  428.   tree type;
  429.   int dist;
  430.   tree rval = NULL_TREE;
  431.   
  432.   if (TREE_CODE (parent) == TREE_VEC)
  433.     parent = BINFO_TYPE (parent);
  434.   else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
  435.     my_friendly_abort (89);
  436.  
  437.   if (TREE_CODE (binfo) == TREE_VEC)
  438.     type = BINFO_TYPE (binfo);
  439.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
  440.     type = binfo;
  441.   else
  442.     my_friendly_abort (90);
  443.   
  444.   dist = get_base_distance (parent, binfo, protect, &rval);
  445.  
  446.   if (dist == -3)
  447.     {
  448.       cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
  449.         parent, type);
  450.       return error_mark_node;
  451.     }
  452.   else if (dist == -2 && protect)
  453.     {
  454.       cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
  455.         type);
  456.       return error_mark_node;
  457.     }
  458.  
  459.   return rval;
  460. }
  461.  
  462. /* This is the newer depth first get_base_distance routine.  */
  463. static int
  464. get_base_distance_recursive (binfo, depth, is_private, basetype_path, rval,
  465.                  rval_private_ptr, new_binfo_ptr, parent, path_ptr,
  466.                  protect, via_virtual_ptr, via_virtual)
  467.      tree binfo, basetype_path, *new_binfo_ptr, parent, *path_ptr;
  468.      int *rval_private_ptr, depth, is_private, rval, protect, *via_virtual_ptr,
  469.        via_virtual;
  470. {
  471.   tree binfos;
  472.   int i, n_baselinks;
  473.  
  474.   if (BINFO_TYPE (binfo) == parent || binfo == parent)
  475.     {
  476.       if (rval == -1)
  477.     {
  478.       rval = depth;
  479.       *rval_private_ptr = is_private;
  480.       *new_binfo_ptr = binfo;
  481.       *via_virtual_ptr = via_virtual;
  482.     }
  483.       else
  484.     {
  485.       int same_object = (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
  486.                          BINFO_OFFSET (binfo))
  487.                  && *via_virtual_ptr && via_virtual);
  488.                  
  489.       if (*via_virtual_ptr && via_virtual==0)
  490.         {
  491.           *rval_private_ptr = is_private;
  492.           *new_binfo_ptr = binfo;
  493.           *via_virtual_ptr = via_virtual;
  494.         }
  495.       else if (same_object)
  496.         {
  497.           if (*rval_private_ptr && ! is_private)
  498.         {
  499.           *rval_private_ptr = is_private;
  500.           *new_binfo_ptr = binfo;
  501.           *via_virtual_ptr = via_virtual;
  502.         }
  503.           return rval;
  504.         }
  505.  
  506.       rval = -2;
  507.     }
  508.       return rval;
  509.     }
  510.  
  511.   binfos = BINFO_BASETYPES (binfo);
  512.   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  513.   depth += 1;
  514.  
  515.   /* Process base types.  */
  516.   for (i = 0; i < n_baselinks; i++)
  517.     {
  518.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  519.  
  520.       /* Find any specific instance of a virtual base, when searching with
  521.      a binfo... */
  522.       if (BINFO_MARKED (base_binfo) == 0 || TREE_CODE (parent) == TREE_VEC)
  523.     {
  524.       int via_private
  525.         = (protect
  526.            && (is_private
  527.            || (!TREE_VIA_PUBLIC (base_binfo)
  528.                && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
  529.       int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
  530.       int was;
  531.  
  532.       /* When searching for a non-virtual, we cannot mark
  533.          virtually found binfos. */
  534.       if (! this_virtual)
  535.         SET_BINFO_MARKED (base_binfo);
  536.  
  537. #define WATCH_VALUES(rval, via_private) (rval == -1 ? 3 : via_private)
  538.  
  539.       was = WATCH_VALUES (rval, *via_virtual_ptr);
  540.       rval = get_base_distance_recursive (base_binfo, depth, via_private,
  541.                           binfo, rval, rval_private_ptr,
  542.                           new_binfo_ptr, parent, path_ptr,
  543.                           protect, via_virtual_ptr,
  544.                           this_virtual);
  545.       /* watch for updates; only update if path is good. */
  546.       if (path_ptr && WATCH_VALUES (rval, *via_virtual_ptr) != was)
  547.         BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
  548.       if (rval == -2 && *via_virtual_ptr == 0)
  549.         return rval;
  550.  
  551. #undef WATCH_VALUES
  552.  
  553.     }
  554.     }
  555.  
  556.   return rval;
  557. }
  558.  
  559. /* Return the number of levels between type PARENT and the type given
  560.    in BINFO, following the leftmost path to PARENT not found along a
  561.    virtual path, if there are no real PARENTs (all come from virtual
  562.    base classes), then follow the leftmost path to PARENT.
  563.  
  564.    Return -1 if TYPE is not derived from PARENT.
  565.    Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
  566.     non-negative.
  567.    Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
  568.  
  569.    If PATH_PTR is non-NULL, then also build the list of types
  570.    from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
  571.    set.
  572.  
  573.    PARENT can also be a binfo, in which case that exact parent is found
  574.    and no other.  convert_pointer_to_real uses this functionality.
  575.  
  576.    If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone.  */
  577.  
  578. int
  579. get_base_distance (parent, binfo, protect, path_ptr)
  580.      register tree parent, binfo;
  581.      int protect;
  582.      tree *path_ptr;
  583. {
  584.   int rval;
  585.   int rval_private = 0;
  586.   tree type;
  587.   tree new_binfo = NULL_TREE;
  588.   int via_virtual;
  589.   int watch_access = protect;
  590.  
  591.   if (TREE_CODE (parent) != TREE_VEC)
  592.     parent = TYPE_MAIN_VARIANT (parent);
  593.  
  594.   if (TREE_CODE (binfo) == TREE_VEC)
  595.     type = BINFO_TYPE (binfo);
  596.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
  597.     {
  598.       type = binfo;
  599.       binfo = TYPE_BINFO (type);
  600.  
  601.       if (path_ptr)
  602.     BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
  603.     }
  604.   else
  605.     my_friendly_abort (92);
  606.  
  607.   if (parent == type || parent == binfo)
  608.     {
  609.       /* If the distance is 0, then we don't really need
  610.      a path pointer, but we shouldn't let garbage go back.  */
  611.       if (path_ptr)
  612.     *path_ptr = binfo;
  613.       return 0;
  614.     }
  615.  
  616.   if (path_ptr)
  617.     watch_access = 1;
  618.  
  619.   rval = get_base_distance_recursive (binfo, 0, 0, NULL_TREE, -1,
  620.                       &rval_private, &new_binfo, parent,
  621.                       path_ptr, watch_access, &via_virtual, 0);
  622.  
  623.   dfs_walk (binfo, dfs_unmark, markedp);
  624.  
  625.   /* Access restrictions don't count if we found an ambiguous basetype.  */
  626.   if (rval == -2 && protect >= 0)
  627.     rval_private = 0;
  628.  
  629.   if (rval && protect && rval_private)
  630.     return -3;
  631.  
  632.   /* find real virtual base classes. */
  633.   if (rval == -1 && TREE_CODE (parent) == TREE_VEC
  634.       && parent == binfo_member (BINFO_TYPE (parent),
  635.                  CLASSTYPE_VBASECLASSES (type)))
  636.     {
  637.       BINFO_INHERITANCE_CHAIN (parent) = binfo;
  638.       new_binfo = parent;
  639.       rval = 1;
  640.     }
  641.  
  642.   if (path_ptr)
  643.     *path_ptr = new_binfo;
  644.   return rval;
  645. }
  646.  
  647. /* Search for a member with name NAME in a multiple inheritance lattice
  648.    specified by TYPE.  If it does not exist, return NULL_TREE.
  649.    If the member is ambiguously referenced, return `error_mark_node'.
  650.    Otherwise, return the FIELD_DECL.  */
  651.  
  652. /* Do a 1-level search for NAME as a member of TYPE.  The caller must
  653.    figure out whether it can access this field.  (Since it is only one
  654.    level, this is reasonable.)  */
  655. static tree
  656. lookup_field_1 (type, name)
  657.      tree type, name;
  658. {
  659.   register tree field = TYPE_FIELDS (type);
  660.  
  661. #ifdef GATHER_STATISTICS
  662.   n_calls_lookup_field_1++;
  663. #endif
  664.   while (field)
  665.     {
  666. #ifdef GATHER_STATISTICS
  667.       n_fields_searched++;
  668. #endif
  669.       if (DECL_NAME (field) == NULL_TREE
  670.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  671.     {
  672.       tree temp = lookup_field_1 (TREE_TYPE (field), name);
  673.       if (temp)
  674.         return temp;
  675.     }
  676.       if (DECL_NAME (field) == name)
  677.     {
  678.       if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
  679.           && DECL_ASSEMBLER_NAME (field) != NULL)
  680.         GNU_xref_ref(current_function_decl,
  681.              IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
  682.       return field;
  683.     }
  684.       field = TREE_CHAIN (field);
  685.     }
  686.   /* Not found.  */
  687.   if (name == _vptr_name)
  688.     {
  689.       /* Give the user what s/he thinks s/he wants.  */
  690.       if (TYPE_VIRTUAL_P (type))
  691.     return CLASSTYPE_VFIELD (type);
  692.     }
  693.   return NULL_TREE;
  694. }
  695.  
  696. /* There are a number of cases we need to be aware of here:
  697.              current_class_type    current_function_decl
  698.    * global            NULL            NULL
  699.    * fn-local            NULL            SET
  700.    * class-local        SET            NULL
  701.    * class->fn            SET            SET
  702.    * fn->class            SET            SET
  703.  
  704.    Those last two make life interesting.  If we're in a function which is
  705.    itself inside a class, we need decls to go into the fn's decls (our
  706.    second case below).  But if we're in a class and the class itself is
  707.    inside a function, we need decls to go into the decls for the class.  To
  708.    achieve this last goal, we must see if, when both current_class_decl and
  709.    current_function_decl are set, the class was declared inside that
  710.    function.  If so, we know to put the decls into the class's scope.  */
  711.  
  712. tree
  713. current_scope ()
  714. {
  715.   if (current_function_decl == NULL_TREE)
  716.     return current_class_type;
  717.   if (current_class_type == NULL_TREE)
  718.     return current_function_decl;
  719.   if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
  720.     return current_function_decl;
  721.  
  722.   return current_class_type;
  723. }
  724.  
  725. /* Compute the access of FIELD.  This is done by computing
  726.    the access available to each type in BASETYPES (which comes
  727.    as a list of [via_public/basetype] in reverse order, namely base
  728.    class before derived class).  The first one which defines a
  729.    access defines the access for the field.  Otherwise, the
  730.    access of the field is that which occurs normally.
  731.  
  732.    Uses global variables CURRENT_CLASS_TYPE and
  733.    CURRENT_FUNCTION_DECL to use friend relationships
  734.    if necessary.
  735.  
  736.    This will be static when lookup_fnfield comes into this file.
  737.  
  738.    access_public means that the field can be accessed by the current lexical
  739.    scope.
  740.  
  741.    access_protected means that the field cannot be accessed by the current
  742.    lexical scope because it is protected.
  743.  
  744.    access_private means that the field cannot be accessed by the current
  745.    lexical scope because it is private. */
  746.  
  747. #if 0
  748. #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public
  749. #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected
  750. #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private
  751. #else
  752. #define PUBLIC_RETURN return access_public
  753. #define PROTECTED_RETURN return access_protected
  754. #define PRIVATE_RETURN return access_private
  755. #endif
  756.  
  757. #if 0
  758. /* Disabled with DECL_PUBLIC &c.  */
  759. static tree previous_scope = NULL_TREE;
  760. #endif
  761.  
  762. enum access_type
  763. compute_access (basetype_path, field)
  764.      tree basetype_path, field;
  765. {
  766.   enum access_type access;
  767.   tree types;
  768.   tree context;
  769.   int protected_ok, via_protected;
  770.   extern int flag_access_control;
  771. #if 1
  772.   /* Replaces static decl above.  */
  773.   tree previous_scope;
  774. #endif
  775.   int static_mem =
  776.     ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field))
  777.      || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field)));
  778.  
  779.   if (! flag_access_control)
  780.     return access_public;
  781.  
  782.   /* The field lives in the current class.  */
  783.   if (BINFO_TYPE (basetype_path) == current_class_type)
  784.     return access_public;
  785.  
  786. #if 0
  787.   /* Disabled until pushing function scope clears these out.  If ever.  */
  788.   /* Make these special cases fast.  */
  789.   if (current_scope () == previous_scope)
  790.     {
  791.       if (DECL_PUBLIC (field))
  792.     return access_public;
  793.       if (DECL_PROTECTED (field))
  794.     return access_protected;
  795.       if (DECL_PRIVATE (field))
  796.     return access_private;
  797.     }
  798. #endif
  799.  
  800.   /* We don't currently support access control on nested types.  */
  801.   if (TREE_CODE (field) == TYPE_DECL)
  802.     return access_public;
  803.  
  804.   previous_scope = current_scope ();
  805.   
  806.   context = DECL_CLASS_CONTEXT (field);
  807.   if (context == NULL_TREE)
  808.     context = DECL_CONTEXT (field);
  809.  
  810.   /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
  811.      slot set to the union type rather than the record type containing
  812.      the anonymous union.  In this case, DECL_FIELD_CONTEXT is correct.  */
  813.   if (context && TREE_CODE (context) == UNION_TYPE
  814.       && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
  815.     context = DECL_FIELD_CONTEXT (field);
  816.  
  817.   /* Virtual function tables are never private.  But we should know that
  818.      we are looking for this, and not even try to hide it.  */
  819.   if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
  820.     PUBLIC_RETURN;
  821.  
  822.   /* Member found immediately within object.  */
  823.   if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
  824.     {
  825.       /* Are we (or an enclosing scope) friends with the class that has
  826.          FIELD? */
  827.       if (is_friend (context, previous_scope))
  828.     PUBLIC_RETURN;
  829.  
  830.       /* If it's private, it's private, you letch.  */
  831.       if (TREE_PRIVATE (field))
  832.     PRIVATE_RETURN;
  833.  
  834.       /* ARM $11.5.  Member functions of a derived class can access the
  835.      non-static protected members of a base class only through a
  836.      pointer to the derived class, a reference to it, or an object
  837.      of it. Also any subsequently derived classes also have
  838.      access.  */
  839.       else if (TREE_PROTECTED (field))
  840.     {
  841.       if (current_class_type
  842.           && static_mem
  843.             && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
  844.         PUBLIC_RETURN;
  845.       else
  846.         PROTECTED_RETURN;
  847.     }
  848.       else
  849.     PUBLIC_RETURN;
  850.     }
  851.  
  852.   /* must reverse more than one element */
  853.   basetype_path = reverse_path (basetype_path);
  854.   types = basetype_path;
  855.   via_protected = 0;
  856.   access = access_default;
  857.   protected_ok = static_mem && current_class_type
  858.     && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
  859.  
  860.   while (1)
  861.     {
  862.       tree member;
  863.       tree binfo = types;
  864.       tree type = BINFO_TYPE (binfo);
  865.       int private_ok = 0;
  866.  
  867.       /* Friends of a class can see protected members of its bases.
  868.          Note that classes are their own friends.  */
  869.       if (is_friend (type, previous_scope))
  870.     {
  871.       protected_ok = 1;
  872.       private_ok = 1;
  873.     }
  874.  
  875.       member = purpose_member (type, DECL_ACCESS (field));
  876.       if (member)
  877.     {
  878.       access = (enum access_type) TREE_VALUE (member);
  879.       break;
  880.     }
  881.  
  882.       types = BINFO_INHERITANCE_CHAIN (types);
  883.  
  884.       /* If the next type was VIA_PROTECTED, then fields of all remaining
  885.      classes past that one are *at least* protected.  */
  886.       if (types)
  887.     {
  888.       if (TREE_VIA_PROTECTED (types))
  889.         via_protected = 1;
  890.       else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
  891.         {
  892.           access = access_private;
  893.           break;
  894.         }
  895.     }
  896.       else
  897.     break;
  898.     }
  899.   reverse_path (basetype_path);
  900.  
  901.   /* No special visibilities apply.  Use normal rules.  */
  902.  
  903.   if (access == access_default)
  904.     {
  905.       if (is_friend (context, previous_scope))
  906.     access = access_public;
  907.       else if (TREE_PRIVATE (field))
  908.     access = access_private;
  909.       else if (TREE_PROTECTED (field))
  910.     access = access_protected;
  911.       else
  912.     access = access_public;
  913.     }
  914.  
  915.   if (access == access_public && via_protected)
  916.     access = access_protected;
  917.  
  918.   if (access == access_protected && protected_ok)
  919.     access = access_public;
  920.  
  921. #if 0
  922.   if (access == access_public)
  923.     DECL_PUBLIC (field) = 1;
  924.   else if (access == access_protected)
  925.     DECL_PROTECTED (field) = 1;
  926.   else if (access == access_private)
  927.     DECL_PRIVATE (field) = 1;
  928.   else my_friendly_abort (96);
  929. #endif
  930.   return access;
  931. }
  932.  
  933. /* Routine to see if the sub-object denoted by the binfo PARENT can be
  934.    found as a base class and sub-object of the object denoted by
  935.    BINFO.  This routine relies upon binfos not being shared, except
  936.    for binfos for virtual bases.  */
  937. static int
  938. is_subobject_of_p (parent, binfo)
  939.      tree parent, binfo;
  940. {
  941.   tree binfos = BINFO_BASETYPES (binfo);
  942.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  943.  
  944.   if (parent == binfo)
  945.     return 1;
  946.  
  947.   /* Process and/or queue base types.  */
  948.   for (i = 0; i < n_baselinks; i++)
  949.     {
  950.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  951.       if (TREE_VIA_VIRTUAL (base_binfo))
  952.     base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
  953.       if (is_subobject_of_p (parent, base_binfo))
  954.     return 1;
  955.     }
  956.   return 0;
  957. }
  958.  
  959. /* See if a one FIELD_DECL hides another.  This routine is meant to
  960.    correspond to ANSI working paper Sept 17, 1992 10p4.  The two
  961.    binfos given are the binfos corresponding to the particular places
  962.    the FIELD_DECLs are found.  This routine relies upon binfos not
  963.    being shared, except for virtual bases. */
  964. static int
  965. hides (hider_binfo, hidee_binfo)
  966.      tree hider_binfo, hidee_binfo;
  967. {
  968.   /* hider hides hidee, if hider has hidee as a base class and
  969.      the instance of hidee is a sub-object of hider.  The first
  970.      part is always true is the second part is true.
  971.  
  972.      When hider and hidee are the same (two ways to get to the exact
  973.      same member) we consider either one as hiding the other. */
  974.   return is_subobject_of_p (hidee_binfo, hider_binfo);
  975. }
  976.  
  977. /* Very similar to lookup_fnfields_1 but it ensures that at least one
  978.    function was declared inside the class given by TYPE.  It really should
  979.    only return functions that match the given TYPE.  */
  980. static int
  981. lookup_fnfields_here (type, name)
  982.      tree type, name;
  983. {
  984.   int index = lookup_fnfields_1 (type, name);
  985.   tree fndecls;
  986.  
  987.   if (index <= 0)
  988.     return index;
  989.   fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  990.   while (fndecls)
  991.     {
  992.       if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (fndecls))
  993.       == TYPE_MAIN_VARIANT (type))
  994.     return index;
  995.       fndecls = TREE_CHAIN (fndecls);
  996.     }
  997.   return -1;
  998. }
  999.  
  1000. /* Look for a field named NAME in an inheritance lattice dominated by
  1001.    XBASETYPE.  PROTECT is zero if we can avoid computing access
  1002.    information, otherwise it is 1.  WANT_TYPE is 1 when we should only
  1003.    return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
  1004.  
  1005.    It was not clear what should happen if WANT_TYPE is set, and an
  1006.    ambiguity is found.  At least one use (lookup_name) to not see
  1007.    the error.  */
  1008. tree
  1009. lookup_field (xbasetype, name, protect, want_type)
  1010.      register tree xbasetype, name;
  1011.      int protect, want_type;
  1012. {
  1013.   int head = 0, tail = 0;
  1014.   tree rval, rval_binfo = NULL_TREE, rval_binfo_h;
  1015.   tree type, basetype_chain, basetype_path;
  1016.   enum access_type this_v = access_default;
  1017.   tree entry, binfo, binfo_h;
  1018.   enum access_type own_access = access_default;
  1019.   int vbase_name_p = VBASE_NAME_P (name);
  1020.  
  1021.   /* rval_binfo is the binfo associated with the found member, note,
  1022.      this can be set with useful information, even when rval is not
  1023.      set, because it must deal with ALL members, not just non-function
  1024.      members.  It is used for ambiguity checking and the hidden
  1025.      checks.  Whereas rval is only set if a proper (not hidden)
  1026.      non-function member is found.  */
  1027.  
  1028.   /* rval_binfo_h and binfo_h are binfo values used when we perform the
  1029.      hiding checks, as virtual base classes may not be shared.  The strategy
  1030.      is we always go into the the binfo hierarchy owned by TYPE_BINFO of
  1031.      virtual base classes, as we cross virtual base class lines.  This way
  1032.      we know that binfo of a virtual base class will always == itself when
  1033.      found along any line.  (mrs)  */
  1034.  
  1035.   char *errstr = 0;
  1036.  
  1037.   /* Set this to nonzero if we don't know how to compute
  1038.      accurate error messages for access control.  */
  1039.   int index = MEMOIZED_HASH_FN (name);
  1040.  
  1041.   /* If we are looking for a constructor in a templated type, use the
  1042.      unspecialized name, as that is how we store it.  */
  1043.   if (IDENTIFIER_TEMPLATE (name))
  1044.     name = constructor_name (name);
  1045.  
  1046.   if (TREE_CODE (xbasetype) == TREE_VEC)
  1047.     {
  1048.       type = BINFO_TYPE (xbasetype);
  1049.       basetype_path = xbasetype;
  1050.     }
  1051.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
  1052.     {
  1053.       type = xbasetype;
  1054.       basetype_path = TYPE_BINFO (xbasetype);
  1055.       BINFO_VIA_PUBLIC (basetype_path) = 1;
  1056.       BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1057.     }
  1058.   else my_friendly_abort (97);
  1059.  
  1060.   if (CLASSTYPE_MTABLE_ENTRY (type))
  1061.     {
  1062.       tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1063.  
  1064.       while (tem && TREE_PURPOSE (tem) != name)
  1065.     {
  1066.       memoized_fields_searched[0]++;
  1067.       tem = TREE_CHAIN (tem);
  1068.     }
  1069.       if (tem)
  1070.     {
  1071.       if (protect && TREE_TYPE (tem))
  1072.         {
  1073.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1074.              IDENTIFIER_POINTER (name),
  1075.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
  1076.           return error_mark_node;
  1077.         }
  1078.       if (TREE_VALUE (tem) == NULL_TREE)
  1079.         memoized_fast_rejects[0] += 1;
  1080.       else
  1081.         memoized_fast_finds[0] += 1;
  1082.       return TREE_VALUE (tem);
  1083.     }
  1084.     }
  1085.  
  1086. #ifdef GATHER_STATISTICS
  1087.   n_calls_lookup_field++;
  1088. #endif
  1089.   if (protect && flag_memoize_lookups && ! global_bindings_p ())
  1090.     entry = make_memoized_table_entry (type, name, 0);
  1091.   else
  1092.     entry = 0;
  1093.  
  1094.   rval = lookup_field_1 (type, name);
  1095.  
  1096.   if (rval || lookup_fnfields_here (type, name) >= 0)
  1097.     {
  1098.       if (rval)
  1099.     {
  1100.       if (want_type)
  1101.         {
  1102.           if (TREE_CODE (rval) != TYPE_DECL)
  1103.         {
  1104.           rval = purpose_member (name, CLASSTYPE_TAGS (type));
  1105.           if (rval)
  1106.             rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
  1107.         }
  1108.         }
  1109.       else
  1110.         {
  1111.           if (TREE_CODE (rval) == TYPE_DECL
  1112.           && lookup_fnfields_here (type, name) >= 0)
  1113.         rval = NULL_TREE;
  1114.         }
  1115.     }
  1116.  
  1117.       if (protect && rval)
  1118.     {
  1119.       if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
  1120.         this_v = compute_access (basetype_path, rval);
  1121.       if (TREE_CODE (rval) == CONST_DECL)
  1122.         {
  1123.           if (this_v == access_private)
  1124.         errstr = "enum `%D' is a private value of class `%T'";
  1125.           else if (this_v == access_protected)
  1126.         errstr = "enum `%D' is a protected value of class `%T'";
  1127.         }
  1128.       else
  1129.         {
  1130.           if (this_v == access_private)
  1131.         errstr = "member `%D' is a private member of class `%T'";
  1132.           else if (this_v == access_protected)
  1133.         errstr = "member `%D' is a protected member of class `%T'";
  1134.         }
  1135.     }
  1136.  
  1137.       if (entry)
  1138.     {
  1139.       if (errstr)
  1140.         {
  1141.           /* This depends on behavior of lookup_field_1!  */
  1142.           tree error_string = my_build_string (errstr);
  1143.           TREE_TYPE (entry) = error_string;
  1144.         }
  1145.       else
  1146.         {
  1147.           /* Let entry know there is no problem with this access.  */
  1148.           TREE_TYPE (entry) = NULL_TREE;
  1149.         }
  1150.       TREE_VALUE (entry) = rval;
  1151.     }
  1152.  
  1153.       if (errstr && protect)
  1154.     {
  1155.       cp_error (errstr, name, type);
  1156.       return error_mark_node;
  1157.     }
  1158.       return rval;
  1159.     }
  1160.  
  1161.   basetype_chain = build_tree_list (NULL_TREE, basetype_path);
  1162.   TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
  1163.   TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
  1164.   TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
  1165.  
  1166.   /* The ambiguity check relies upon breadth first searching. */
  1167.  
  1168.   search_stack = push_search_level (search_stack, &search_obstack);
  1169.   binfo = basetype_path;
  1170.   binfo_h = binfo;
  1171.  
  1172.   while (1)
  1173.     {
  1174.       tree binfos = BINFO_BASETYPES (binfo);
  1175.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1176.       tree nval;
  1177.  
  1178.       /* Process and/or queue base types.  */
  1179.       for (i = 0; i < n_baselinks; i++)
  1180.     {
  1181.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1182.       if (BINFO_FIELDS_MARKED (base_binfo) == 0)
  1183.         {
  1184.           tree btypes;
  1185.  
  1186.           SET_BINFO_FIELDS_MARKED (base_binfo);
  1187.           btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
  1188.           TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
  1189.           TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
  1190.           TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
  1191.           if (TREE_VIA_VIRTUAL (base_binfo))
  1192.         btypes = tree_cons (NULL_TREE,
  1193.                     TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
  1194.                     btypes);
  1195.           else
  1196.         btypes = tree_cons (NULL_TREE,
  1197.                     TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
  1198.                     btypes);
  1199.           obstack_ptr_grow (&search_obstack, btypes);
  1200.           tail += 1;
  1201.           if (tail >= search_stack->limit)
  1202.         my_friendly_abort (98);
  1203.         }
  1204.     }
  1205.  
  1206.       /* Process head of queue, if one exists.  */
  1207.       if (head >= tail)
  1208.     break;
  1209.  
  1210.       basetype_chain = search_stack->first[head++];
  1211.       binfo_h = TREE_VALUE (basetype_chain);
  1212.       basetype_chain = TREE_CHAIN (basetype_chain);
  1213.       basetype_path = TREE_VALUE (basetype_chain);
  1214.       if (TREE_CHAIN (basetype_chain))
  1215.     BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
  1216.       else
  1217.     BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1218.  
  1219.       binfo = basetype_path;
  1220.       type = BINFO_TYPE (binfo);
  1221.  
  1222.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1223.      and we do find NAME in TYPE, verify that such a second
  1224.      sighting is in fact valid.  */
  1225.  
  1226.       nval = lookup_field_1 (type, name);
  1227.  
  1228.       if (nval || lookup_fnfields_here (type, name)>=0)
  1229.     {
  1230.       if (nval && nval == rval && SHARED_MEMBER_P (nval))
  1231.         {
  1232.           /* This is ok, the member found is the same [class.ambig] */
  1233.         }
  1234.       else if (rval_binfo && hides (rval_binfo_h, binfo_h))
  1235.         {
  1236.           /* This is ok, the member found is in rval_binfo, not
  1237.          here (binfo). */
  1238.         }
  1239.       else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h))
  1240.         {
  1241.           /* This is ok, the member found is here (binfo), not in
  1242.          rval_binfo. */
  1243.           if (nval)
  1244.         {
  1245.           rval = nval;
  1246.           if (entry || protect)
  1247.             this_v = compute_access (basetype_path, rval);
  1248.           /* These may look ambiguous, but they really are not.  */
  1249.           if (vbase_name_p)
  1250.             break;
  1251.         }
  1252.           else
  1253.         {
  1254.           /* Undo finding it before, as something else hides it. */
  1255.           rval = NULL_TREE;
  1256.         }
  1257.           rval_binfo = binfo;
  1258.           rval_binfo_h = binfo_h;
  1259.         }
  1260.       else
  1261.         {
  1262.           /* This is ambiguous. */
  1263.           errstr = "request for member `%D' is ambiguous";
  1264.           protect = 2;
  1265.           break;
  1266.         }
  1267.     }
  1268.     }
  1269.   {
  1270.     tree *tp = search_stack->first;
  1271.     tree *search_tail = tp + tail;
  1272.  
  1273.     if (entry)
  1274.       TREE_VALUE (entry) = rval;
  1275.  
  1276.     if (rval_binfo)
  1277.       {
  1278.     type = BINFO_TYPE (rval_binfo);
  1279.  
  1280.     if (rval)
  1281.       {
  1282.         if (want_type)
  1283.           {
  1284.         if (TREE_CODE (rval) != TYPE_DECL)
  1285.           {
  1286.             rval = purpose_member (name, CLASSTYPE_TAGS (type));
  1287.             if (rval)
  1288.               rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
  1289.           }
  1290.           }
  1291.         else
  1292.           {
  1293.         if (TREE_CODE (rval) == TYPE_DECL
  1294.             && lookup_fnfields_here (type, name) >= 0)
  1295.           rval = NULL_TREE;
  1296.           }
  1297.       }
  1298.       }
  1299.  
  1300.     if (rval == NULL_TREE)
  1301.       errstr = 0;
  1302.  
  1303.     /* If this FIELD_DECL defines its own access level, deal with that.  */
  1304.     if (rval && errstr == 0
  1305.     && ((protect&1) || entry)
  1306.     && DECL_LANG_SPECIFIC (rval)
  1307.     && DECL_ACCESS (rval))
  1308.       {
  1309.     while (tp < search_tail)
  1310.       {
  1311.         /* If is possible for one of the derived types on the path to
  1312.            have defined special access for this field.  Look for such
  1313.            declarations and report an error if a conflict is found.  */
  1314.         enum access_type new_v;
  1315.  
  1316.         if (this_v != access_default)
  1317.           new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval);
  1318.         if (this_v != access_default && new_v != this_v)
  1319.           {
  1320.         errstr = "conflicting access to member `%D'";
  1321.         this_v = access_default;
  1322.           }
  1323.         own_access = new_v;
  1324.         CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1325.         tp += 1;
  1326.       }
  1327.       }
  1328.     else
  1329.       {
  1330.     while (tp < search_tail)
  1331.       {
  1332.         CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1333.         tp += 1;
  1334.       }
  1335.       }
  1336.   }
  1337.   search_stack = pop_search_level (search_stack);
  1338.  
  1339.   if (errstr == 0)
  1340.     {
  1341.       if (own_access == access_private)
  1342.     errstr = "member `%D' declared private";
  1343.       else if (own_access == access_protected)
  1344.     errstr = "member `%D' declared protected";
  1345.       else if (this_v == access_private)
  1346.     errstr = TREE_PRIVATE (rval)
  1347.       ? "member `%D' is private"
  1348.         : "member `%D' is from private base class";
  1349.       else if (this_v == access_protected)
  1350.     errstr = TREE_PROTECTED (rval)
  1351.       ? "member `%D' is protected"
  1352.         : "member `%D' is from protected base class";
  1353.     }
  1354.  
  1355.   if (entry)
  1356.     {
  1357.       if (errstr)
  1358.     {
  1359.       tree error_string = my_build_string (errstr);
  1360.       /* Save error message with entry.  */
  1361.       TREE_TYPE (entry) = error_string;
  1362.     }
  1363.       else
  1364.     {
  1365.       /* Mark entry as having no error string.  */
  1366.       TREE_TYPE (entry) = NULL_TREE;
  1367.     }
  1368.     }
  1369.  
  1370.   if (errstr && protect)
  1371.     {
  1372.       cp_error (errstr, name, type);
  1373.       rval = error_mark_node;
  1374.     }
  1375.   return rval;
  1376. }
  1377.  
  1378. /* Try to find NAME inside a nested class.  */
  1379. tree
  1380. lookup_nested_field (name, complain)
  1381.      tree name;
  1382.      int complain;
  1383. {
  1384.   register tree t;
  1385.  
  1386.   tree id = NULL_TREE;
  1387.   if (TREE_CHAIN (current_class_type))
  1388.     {
  1389.       /* Climb our way up the nested ladder, seeing if we're trying to
  1390.      modify a field in an enclosing class.  If so, we should only
  1391.      be able to modify if it's static.  */
  1392.       for (t = TREE_CHAIN (current_class_type);
  1393.        t && DECL_CONTEXT (t);
  1394.        t = TREE_CHAIN (DECL_CONTEXT (t)))
  1395.     {
  1396.       if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
  1397.         break;
  1398.  
  1399.       /* N.B.: lookup_field will do the access checking for us */
  1400.       id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
  1401.       if (id == error_mark_node)
  1402.         {
  1403.           id = NULL_TREE;
  1404.           continue;
  1405.         }
  1406.  
  1407.       if (id != NULL_TREE)
  1408.         {
  1409.           if (TREE_CODE (id) == FIELD_DECL
  1410.           && ! TREE_STATIC (id)
  1411.           && TREE_TYPE (id) != error_mark_node)
  1412.         {
  1413.           if (complain)
  1414.             {
  1415.               /* At parse time, we don't want to give this error, since
  1416.              we won't have enough state to make this kind of
  1417.              decision properly.  But there are times (e.g., with
  1418.              enums in nested classes) when we do need to call
  1419.              this fn at parse time.  So, in those cases, we pass
  1420.              complain as a 0 and just return a NULL_TREE.  */
  1421.               error ("assignment to non-static member `%s' of enclosing class `%s'",
  1422.                  lang_printable_name (id),
  1423.                  IDENTIFIER_POINTER (TYPE_IDENTIFIER
  1424.                          (DECL_CONTEXT (t))));
  1425.               /* Mark this for do_identifier().  It would otherwise
  1426.              claim that the variable was undeclared.  */
  1427.               TREE_TYPE (id) = error_mark_node;
  1428.             }
  1429.           else
  1430.             {
  1431.               id = NULL_TREE;
  1432.               continue;
  1433.             }
  1434.         }
  1435.           break;
  1436.         }
  1437.     }
  1438.     }
  1439.  
  1440.   return id;
  1441. }
  1442.  
  1443. /* TYPE is a class type. Return the index of the fields within
  1444.    the method vector with name NAME, or -1 is no such field exists.  */
  1445. static int
  1446. lookup_fnfields_1 (type, name)
  1447.      tree type, name;
  1448. {
  1449.   register tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1450.  
  1451.   if (method_vec != 0)
  1452.     {
  1453.       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
  1454.       register tree *end = TREE_VEC_END (method_vec);
  1455.  
  1456. #ifdef GATHER_STATISTICS
  1457.       n_calls_lookup_fnfields_1++;
  1458. #endif
  1459.       if (*methods && name == constructor_name (type))
  1460.     return 0;
  1461.  
  1462.       while (++methods != end)
  1463.     {
  1464. #ifdef GATHER_STATISTICS
  1465.       n_outer_fields_searched++;
  1466. #endif
  1467.       if (DECL_NAME (*methods) == name)
  1468.         break;
  1469.     }
  1470.       if (methods != end)
  1471.     return methods - &TREE_VEC_ELT (method_vec, 0);
  1472.     }
  1473.  
  1474.   return -1;
  1475. }
  1476.  
  1477. /* Starting from BASETYPE, return a TREE_BASELINK-like object
  1478.    which gives the following information (in a list):
  1479.  
  1480.    TREE_TYPE: list of basetypes needed to get to...
  1481.    TREE_VALUE: list of all functions in of given type
  1482.    which have name NAME.
  1483.  
  1484.    No access information is computed by this function,
  1485.    other then to adorn the list of basetypes with
  1486.    TREE_VIA_PUBLIC.
  1487.  
  1488.    If there are two ways to find a name (two members), if COMPLAIN is
  1489.    non-zero, then error_mark_node is returned, and an error message is
  1490.    printed, otherwise, just an error_mark_node is returned.
  1491.  
  1492.    As a special case, is COMPLAIN is -1, we don't complain, and we
  1493.    don't return error_mark_node, but rather the complete list of
  1494.    virtuals.  This is used by get_virtuals_named_this.  */
  1495. tree
  1496. lookup_fnfields (basetype_path, name, complain)
  1497.      tree basetype_path, name;
  1498.      int complain;
  1499. {
  1500.   int head = 0, tail = 0;
  1501.   tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE, rval_binfo_h;
  1502.   tree entry, binfo, basetype_chain, binfo_h;
  1503.   int find_all = 0;
  1504.  
  1505.   /* rval_binfo is the binfo associated with the found member, note,
  1506.      this can be set with useful information, even when rval is not
  1507.      set, because it must deal with ALL members, not just function
  1508.      members.  It is used for ambiguity checking and the hidden
  1509.      checks.  Whereas rval is only set if a proper (not hidden)
  1510.      function member is found.  */
  1511.  
  1512.   /* rval_binfo_h and binfo_h are binfo values used when we perform the
  1513.      hiding checks, as virtual base classes may not be shared.  The strategy
  1514.      is we always go into the the binfo hierarchy owned by TYPE_BINFO of
  1515.      virtual base classes, as we cross virtual base class lines.  This way
  1516.      we know that binfo of a virtual base class will always == itself when
  1517.      found along any line.  (mrs)  */
  1518.  
  1519.   /* For now, don't try this.  */
  1520.   int protect = complain;
  1521.  
  1522.   char *errstr = 0;
  1523.  
  1524.   /* Set this to nonzero if we don't know how to compute
  1525.      accurate error messages for access control.  */
  1526.   int index = MEMOIZED_HASH_FN (name);
  1527.  
  1528.   if (complain == -1)
  1529.     {
  1530.       find_all = 1;
  1531.       protect = complain = 0;
  1532.     }
  1533.  
  1534.   /* If we are looking for a constructor in a templated type, use the
  1535.      unspecialized name, as that is how we store it.  */
  1536.   if (IDENTIFIER_TEMPLATE (name))
  1537.     name = constructor_name (name);
  1538.  
  1539.   binfo = basetype_path;
  1540.   binfo_h = binfo;
  1541.   type = BINFO_TYPE (basetype_path);
  1542.  
  1543.   /* The memoization code is in need of maintenance. */
  1544.   if (!find_all && CLASSTYPE_MTABLE_ENTRY (type))
  1545.     {
  1546.       tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1547.  
  1548.       while (tem && TREE_PURPOSE (tem) != name)
  1549.     {
  1550.       memoized_fields_searched[1]++;
  1551.       tem = TREE_CHAIN (tem);
  1552.     }
  1553.       if (tem)
  1554.     {
  1555.       if (protect && TREE_TYPE (tem))
  1556.         {
  1557.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1558.              IDENTIFIER_POINTER (name),
  1559.              TYPE_NAME_STRING (DECL_CLASS_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
  1560.           return error_mark_node;
  1561.         }
  1562.       if (TREE_VALUE (tem) == NULL_TREE)
  1563.         {
  1564.           memoized_fast_rejects[1] += 1;
  1565.           return NULL_TREE;
  1566.         }
  1567.       else
  1568.         {
  1569.           /* Want to return this, but we must make sure
  1570.          that access information is consistent.  */
  1571.           tree baselink = TREE_VALUE (tem);
  1572.           tree memoized_basetypes = TREE_PURPOSE (baselink);
  1573.           tree these_basetypes = basetype_path;
  1574.           while (memoized_basetypes && these_basetypes)
  1575.         {
  1576.           memoized_fields_searched[1]++;
  1577.           if (TREE_VALUE (memoized_basetypes) != these_basetypes)
  1578.             break;
  1579.           memoized_basetypes = TREE_CHAIN (memoized_basetypes);
  1580.           these_basetypes = BINFO_INHERITANCE_CHAIN (these_basetypes);
  1581.         }
  1582.           /* The following statement is true only when both are NULL.  */
  1583.           if (memoized_basetypes == these_basetypes)
  1584.         {
  1585.           memoized_fast_finds[1] += 1;
  1586.           return TREE_VALUE (tem);
  1587.         }
  1588.           /* else, we must re-find this field by hand.  */
  1589.           baselink = tree_cons (basetype_path, TREE_VALUE (baselink), TREE_CHAIN (baselink));
  1590.           return baselink;
  1591.         }
  1592.     }
  1593.     }
  1594.  
  1595. #ifdef GATHER_STATISTICS
  1596.   n_calls_lookup_fnfields++;
  1597. #endif
  1598.   if (protect && flag_memoize_lookups && ! global_bindings_p ())
  1599.     entry = make_memoized_table_entry (type, name, 1);
  1600.   else
  1601.     entry = 0;
  1602.  
  1603.   index = lookup_fnfields_here (type, name);
  1604.   if (index >= 0 || lookup_field_1 (type, name))
  1605.     {
  1606.       rval_binfo = basetype_path;
  1607.       rval_binfo_h = rval_binfo;
  1608.     }
  1609.  
  1610.   if (index >= 0)
  1611.     {
  1612.       rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1613.       rvals = my_tree_cons (basetype_path, rval, rvals);
  1614.       if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
  1615.     TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1616.  
  1617.       if (entry)
  1618.     {
  1619.       TREE_VALUE (entry) = rvals;
  1620.       TREE_TYPE (entry) = NULL_TREE;
  1621.     }
  1622.  
  1623.       return rvals;
  1624.     }
  1625.   rval = NULL_TREE;
  1626.  
  1627.   if (basetype_path == TYPE_BINFO (type))
  1628.     {
  1629.       basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
  1630.       TREE_VIA_PUBLIC (basetype_chain) = 1;
  1631.       BINFO_VIA_PUBLIC (basetype_path) = 1;
  1632.       BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1633.     }
  1634.   else
  1635.     {
  1636.       basetype_chain = build_tree_list (NULL_TREE, basetype_path);
  1637.       TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
  1638.       TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
  1639.       TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
  1640.     }
  1641.  
  1642.   /* The ambiguity check relies upon breadth first searching. */
  1643.  
  1644.   search_stack = push_search_level (search_stack, &search_obstack);
  1645.   binfo = basetype_path;
  1646.   binfo_h = binfo;
  1647.  
  1648.   while (1)
  1649.     {
  1650.       tree binfos = BINFO_BASETYPES (binfo);
  1651.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1652.       int index;
  1653.  
  1654.       /* Process and/or queue base types.  */
  1655.       for (i = 0; i < n_baselinks; i++)
  1656.     {
  1657.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1658.       if (BINFO_FIELDS_MARKED (base_binfo) == 0)
  1659.         {
  1660.           tree btypes;
  1661.  
  1662.           SET_BINFO_FIELDS_MARKED (base_binfo);
  1663.           btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
  1664.           TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
  1665.           TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
  1666.           TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
  1667.           if (TREE_VIA_VIRTUAL (base_binfo))
  1668.         btypes = tree_cons (NULL_TREE,
  1669.                     TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
  1670.                     btypes);
  1671.           else
  1672.         btypes = tree_cons (NULL_TREE,
  1673.                     TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
  1674.                     btypes);
  1675.           obstack_ptr_grow (&search_obstack, btypes);
  1676.           tail += 1;
  1677.           if (tail >= search_stack->limit)
  1678.         my_friendly_abort (99);
  1679.         }
  1680.     }
  1681.  
  1682.       /* Process head of queue, if one exists.  */
  1683.       if (head >= tail)
  1684.     break;
  1685.  
  1686.       basetype_chain = search_stack->first[head++];
  1687.       binfo_h = TREE_VALUE (basetype_chain);
  1688.       basetype_chain = TREE_CHAIN (basetype_chain);
  1689.       basetype_path = TREE_VALUE (basetype_chain);
  1690.       if (TREE_CHAIN (basetype_chain))
  1691.     BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
  1692.       else
  1693.     BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1694.  
  1695.       binfo = basetype_path;
  1696.       type = BINFO_TYPE (binfo);
  1697.  
  1698.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1699.      and we do find NAME in TYPE, verify that such a second
  1700.      sighting is in fact valid.  */
  1701.  
  1702.       index = lookup_fnfields_here (type, name);
  1703.  
  1704.       if (index >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
  1705.     {
  1706.       if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
  1707.         {
  1708.           /* This is ok, the member found is in rval_binfo, not
  1709.          here (binfo). */
  1710.         }
  1711.       else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
  1712.         {
  1713.           /* This is ok, the member found is here (binfo), not in
  1714.          rval_binfo. */
  1715.           if (index >= 0)
  1716.         {
  1717.           rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1718.           /* Note, rvals can only be previously set if find_all is
  1719.              true.  */
  1720.           rvals = my_tree_cons (basetype_path, rval, rvals);
  1721.           if (TYPE_BINFO_BASETYPES (type)
  1722.               && CLASSTYPE_BASELINK_VEC (type))
  1723.             TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1724.         }
  1725.           else
  1726.         {
  1727.           /* Undo finding it before, as something else hides it. */
  1728.           rval = NULL_TREE;
  1729.           rvals = NULL_TREE;
  1730.         }
  1731.           rval_binfo = binfo;
  1732.           rval_binfo_h = binfo_h;
  1733.         }
  1734.       else
  1735.         {
  1736.           /* This is ambiguous. */
  1737.           errstr = "request for method `%D' is ambiguous";
  1738.           rvals = error_mark_node;
  1739.           break;
  1740.         }
  1741.     }
  1742.     }
  1743.   {
  1744.     tree *tp = search_stack->first;
  1745.     tree *search_tail = tp + tail;
  1746.  
  1747.     while (tp < search_tail)
  1748.       {
  1749.     CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1750.     tp += 1;
  1751.       }
  1752.   }
  1753.   search_stack = pop_search_level (search_stack);
  1754.  
  1755.   if (entry)
  1756.     {
  1757.       if (errstr)
  1758.     {
  1759.       tree error_string = my_build_string (errstr);
  1760.       /* Save error message with entry.  */
  1761.       TREE_TYPE (entry) = error_string;
  1762.     }
  1763.       else
  1764.     {
  1765.       /* Mark entry as having no error string.  */
  1766.       TREE_TYPE (entry) = NULL_TREE;
  1767.       TREE_VALUE (entry) = rvals;
  1768.     }
  1769.     }
  1770.  
  1771.   if (errstr && protect)
  1772.     {
  1773.       cp_error (errstr, name);
  1774.       rvals = error_mark_node;
  1775.     }
  1776.  
  1777.   return rvals;
  1778. }
  1779.  
  1780. /* BREADTH-FIRST SEARCH ROUTINES.  */
  1781.  
  1782. /* Search a multiple inheritance hierarchy by breadth-first search.
  1783.  
  1784.    TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
  1785.    TESTFN is a function, which, if true, means that our condition has been met,
  1786.    and its return value should be returned.
  1787.    QFN, if non-NULL, is a predicate dictating whether the type should
  1788.    even be queued.  */
  1789.  
  1790. HOST_WIDE_INT
  1791. breadth_first_search (binfo, testfn, qfn)
  1792.      tree binfo;
  1793.      int (*testfn)();
  1794.      int (*qfn)();
  1795. {
  1796.   int head = 0, tail = 0;
  1797.   int rval = 0;
  1798.  
  1799.   search_stack = push_search_level (search_stack, &search_obstack);
  1800.  
  1801.   while (1)
  1802.     {
  1803.       tree binfos = BINFO_BASETYPES (binfo);
  1804.       int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1805.       int i;
  1806.  
  1807.       /* Process and/or queue base types.  */
  1808.       for (i = 0; i < n_baselinks; i++)
  1809.     {
  1810.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1811.  
  1812.       if (BINFO_MARKED (base_binfo) == 0
  1813.           && (qfn == 0 || (*qfn) (binfo, i)))
  1814.         {
  1815.           SET_BINFO_MARKED (base_binfo);
  1816.           obstack_ptr_grow (&search_obstack, binfo);
  1817.           obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i);
  1818.           tail += 2;
  1819.           if (tail >= search_stack->limit)
  1820.         my_friendly_abort (100);
  1821.         }
  1822.     }
  1823.       /* Process head of queue, if one exists.  */
  1824.       if (head >= tail)
  1825.     {
  1826.       rval = 0;
  1827.       break;
  1828.     }
  1829.  
  1830.       binfo = search_stack->first[head++];
  1831.       i = (HOST_WIDE_INT) search_stack->first[head++];
  1832.       if (rval = (*testfn) (binfo, i))
  1833.     break;
  1834.       binfo = BINFO_BASETYPE (binfo, i);
  1835.     }
  1836.   {
  1837.     tree *tp = search_stack->first;
  1838.     tree *search_tail = tp + tail;
  1839.     while (tp < search_tail)
  1840.       {
  1841.     tree binfo = *tp++;
  1842.     int i = (HOST_WIDE_INT)(*tp++);
  1843.     CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i));
  1844.       }
  1845.   }
  1846.  
  1847.   search_stack = pop_search_level (search_stack);
  1848.   return rval;
  1849. }
  1850.  
  1851. /* Functions to use in breadth first searches.  */
  1852. typedef tree (*pft)();
  1853. typedef int (*pfi)();
  1854.  
  1855. int tree_needs_constructor_p (binfo, i)
  1856.      tree binfo;
  1857.      int i;
  1858. {
  1859.   tree basetype;
  1860.   my_friendly_assert (i != 0, 296);
  1861.   basetype = BINFO_TYPE (BINFO_BASETYPE (binfo, i));
  1862.   return TYPE_NEEDS_CONSTRUCTING (basetype);
  1863. }
  1864.  
  1865. static tree declarator;
  1866.  
  1867. static tree
  1868. get_virtuals_named_this (binfo)
  1869.      tree binfo;
  1870. {
  1871.   tree fields;
  1872.  
  1873.   fields = lookup_fnfields (binfo, declarator, -1);
  1874.   /* fields cannot be error_mark_node */
  1875.  
  1876.   if (fields == 0)
  1877.     return 0;
  1878.  
  1879.   /* Get to the function decls, and return the first virtual function
  1880.      with this name, if there is one.  */
  1881.   while (fields)
  1882.     {
  1883.       tree fndecl;
  1884.  
  1885.       for (fndecl = TREE_VALUE (fields); fndecl; fndecl = DECL_CHAIN (fndecl))
  1886.     if (DECL_VINDEX (fndecl))
  1887.       return fields;
  1888.       fields = next_baselink (fields);
  1889.     }
  1890.   return NULL_TREE;
  1891. }
  1892.  
  1893. static tree get_virtual_destructor (binfo, i)
  1894.      tree binfo;
  1895.      int i;
  1896. {
  1897.   tree type = BINFO_TYPE (binfo);
  1898.   if (i >= 0)
  1899.     type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
  1900.   if (TYPE_HAS_DESTRUCTOR (type)
  1901.       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
  1902.     return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  1903.   return 0;
  1904. }
  1905.  
  1906. int tree_has_any_destructor_p (binfo, i)
  1907.      tree binfo;
  1908.      int i;
  1909. {
  1910.   tree type = BINFO_TYPE (binfo);
  1911.   if (i >= 0)
  1912.     type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
  1913.   return TYPE_NEEDS_DESTRUCTOR (type);
  1914. }
  1915.  
  1916. /* Given a class type TYPE, and a function decl FNDECL, look for a
  1917.    virtual function in TYPE's hierarchy which FNDECL could match as a
  1918.    virtual function.  It doesn't matter which one we find.
  1919.  
  1920.    DTORP is nonzero if we are looking for a destructor.  Destructors
  1921.    need special treatment because they do not match by name.  */
  1922. tree
  1923. get_matching_virtual (binfo, fndecl, dtorp)
  1924.      tree binfo, fndecl;
  1925.      int dtorp;
  1926. {
  1927.   tree tmp = NULL_TREE;
  1928.  
  1929.   /* Breadth first search routines start searching basetypes
  1930.      of TYPE, so we must perform first ply of search here.  */
  1931.   if (dtorp)
  1932.     {
  1933.       if (tree_has_any_destructor_p (binfo, -1))
  1934.     tmp = get_virtual_destructor (binfo, -1);
  1935.  
  1936.       if (tmp)
  1937.     return tmp;
  1938.  
  1939.       tmp = (tree) breadth_first_search (binfo,
  1940.                      (pfi) get_virtual_destructor,
  1941.                      tree_has_any_destructor_p);
  1942.       return tmp;
  1943.     }
  1944.   else
  1945.     {
  1946.       tree drettype, dtypes, btypes, instptr_type;
  1947.       tree basetype = DECL_CLASS_CONTEXT (fndecl);
  1948.       tree baselink, best = NULL_TREE;
  1949.       tree name = DECL_ASSEMBLER_NAME (fndecl);
  1950.  
  1951.       declarator = DECL_NAME (fndecl);
  1952.       if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
  1953.     return NULL_TREE;
  1954.  
  1955.       baselink = get_virtuals_named_this (binfo);
  1956.       if (baselink == NULL_TREE)
  1957.     return NULL_TREE;
  1958.  
  1959.       drettype = TREE_TYPE (TREE_TYPE (fndecl));
  1960.       dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  1961.       if (DECL_STATIC_FUNCTION_P (fndecl))
  1962.     instptr_type = NULL_TREE;
  1963.       else
  1964.     instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
  1965.  
  1966.       for (; baselink; baselink = next_baselink (baselink))
  1967.     {
  1968.       for (tmp = TREE_VALUE (baselink); tmp; tmp = DECL_CHAIN (tmp))
  1969.         {
  1970.           if (! DECL_VINDEX (tmp))
  1971.         continue;
  1972.  
  1973.           btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
  1974.           if (instptr_type == NULL_TREE)
  1975.         {
  1976.           if (compparms (TREE_CHAIN (btypes), dtypes, 3))
  1977.             /* Caller knows to give error in this case.  */
  1978.             return tmp;
  1979.           return NULL_TREE;
  1980.         }
  1981.  
  1982.           if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
  1983.            == TYPE_READONLY (instptr_type))
  1984.           && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
  1985.         {
  1986.           tree brettype = TREE_TYPE (TREE_TYPE (tmp));
  1987.           if (comptypes (brettype, drettype, 1))
  1988.             /* OK */;
  1989.           else if
  1990.             (TREE_CODE (brettype) == TREE_CODE (drettype)
  1991.              && (TREE_CODE (brettype) == POINTER_TYPE
  1992.              || TREE_CODE (brettype) == REFERENCE_TYPE)
  1993.              && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (brettype)),
  1994.                    TYPE_MAIN_VARIANT (TREE_TYPE (drettype)),
  1995.                    0))
  1996.               /* covariant return type */
  1997.             {
  1998.               tree b = TREE_TYPE (brettype), d = TREE_TYPE (drettype);
  1999.               if (TYPE_MAIN_VARIANT (b) != TYPE_MAIN_VARIANT (d))
  2000.             {
  2001.               tree binfo = get_binfo (b, d, 1);
  2002.               if (binfo != error_mark_node
  2003.                   && ! BINFO_OFFSET_ZEROP (binfo))
  2004.                 sorry ("adjusting pointers for covariant returns");
  2005.             }
  2006.               if (TYPE_READONLY (d) > TYPE_READONLY (b))
  2007.             {
  2008.               cp_error ("return type of `%#D' adds const", fndecl);
  2009.               cp_error_at ("  overriding definition as `%#D'",
  2010.                        tmp);
  2011.             }
  2012.               else if (TYPE_VOLATILE (d) > TYPE_VOLATILE (b))
  2013.             {
  2014.               cp_error ("return type of `%#D' adds volatile",
  2015.                     fndecl);
  2016.               cp_error_at ("  overriding definition as `%#D'",
  2017.                        tmp);
  2018.             }
  2019.             }
  2020.           else if (IS_AGGR_TYPE_2 (brettype, drettype)
  2021.                && comptypes (brettype, drettype, 0))
  2022.             {
  2023.               error ("invalid covariant return type (must use pointer or reference)");
  2024.               cp_error_at ("  overriding `%#D'", tmp);
  2025.               cp_error ("  with `%#D'", fndecl);
  2026.             }
  2027.           else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
  2028.             {
  2029.               cp_error ("conflicting return type specified for virtual function `%#D'", fndecl);
  2030.               cp_error_at ("  overriding definition as `%#D'", tmp);
  2031.               SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  2032.             }
  2033.           break;
  2034.         }
  2035.         }
  2036.       if (tmp)
  2037.         {
  2038.           best = tmp;
  2039.           break;
  2040.         }
  2041.     }
  2042.       if (best == NULL_TREE && warn_overloaded_virtual)
  2043.     cp_warning_at ("conflicting specification deriving virtual function `%D'", fndecl);
  2044.  
  2045.       return best;
  2046.     }
  2047. }
  2048.  
  2049. /* Return the list of virtual functions which are abstract in type
  2050.    TYPE that come from non virtual base classes.  See
  2051.    expand_direct_vtbls_init for the style of search we do.  */
  2052. static tree
  2053. get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
  2054.      tree binfo, abstract_virtuals;
  2055.      int do_self;
  2056. {
  2057.   tree binfos = BINFO_BASETYPES (binfo);
  2058.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2059.  
  2060.   for (i = 0; i < n_baselinks; i++)
  2061.     {
  2062.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2063.       int is_not_base_vtable =
  2064.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2065.       if (! TREE_VIA_VIRTUAL (base_binfo))
  2066.     abstract_virtuals
  2067.       = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
  2068.                      abstract_virtuals);
  2069.     }
  2070.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2071.   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2072.     {
  2073.       /* Get around first entry reserved for RTTI.  */
  2074.       tree tmp = TREE_CHAIN (BINFO_VIRTUALS (binfo));
  2075.  
  2076.       while (tmp)
  2077.     {
  2078.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  2079.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  2080.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  2081.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  2082.       tmp = TREE_CHAIN (tmp);
  2083.     }
  2084.     }
  2085.   return abstract_virtuals;
  2086. }
  2087.  
  2088. /* Return the list of virtual functions which are abstract in type TYPE.
  2089.    This information is cached, and so must be built on a
  2090.    non-temporary obstack.  */
  2091. tree
  2092. get_abstract_virtuals (type)
  2093.      tree type;
  2094. {
  2095.   tree vbases, tmp;
  2096.   tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  2097.  
  2098.   /* First get all from non-virtual bases. */
  2099.   abstract_virtuals
  2100.     = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
  2101.                            
  2102.   for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
  2103.     {
  2104.       if (! BINFO_VIRTUALS (vbases))
  2105.     continue;
  2106.  
  2107.       tmp = TREE_CHAIN (BINFO_VIRTUALS (vbases));
  2108.       while (tmp)
  2109.     {
  2110.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  2111.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  2112.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  2113.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  2114.       tmp = TREE_CHAIN (tmp);
  2115.     }
  2116.     }
  2117.   return nreverse (abstract_virtuals);
  2118. }
  2119.  
  2120. /* For the type TYPE, return a list of member functions available from
  2121.    base classes with name NAME.  The TREE_VALUE of the list is a chain of
  2122.    member functions with name NAME.  The TREE_PURPOSE of the list is a
  2123.    basetype, or a list of base types (in reverse order) which were
  2124.    traversed to reach the chain of member functions.  If we reach a base
  2125.    type which provides a member function of name NAME, and which has at
  2126.    most one base type itself, then we can terminate the search.  */
  2127.  
  2128. tree
  2129. get_baselinks (type_as_binfo_list, type, name)
  2130.      tree type_as_binfo_list;
  2131.      tree type, name;
  2132. {
  2133.   int head = 0, tail = 0, index;
  2134.   tree rval = 0, nval = 0;
  2135.   tree basetypes = type_as_binfo_list;
  2136.   tree binfo = TYPE_BINFO (type);
  2137.  
  2138.   search_stack = push_search_level (search_stack, &search_obstack);
  2139.  
  2140.   while (1)
  2141.     {
  2142.       tree binfos = BINFO_BASETYPES (binfo);
  2143.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2144.  
  2145.       /* Process and/or queue base types.  */
  2146.       for (i = 0; i < n_baselinks; i++)
  2147.     {
  2148.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2149.       tree btypes;
  2150.  
  2151.       btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
  2152.                    TREE_VIA_VIRTUAL (base_binfo),
  2153.                    TREE_VIA_PROTECTED (base_binfo),
  2154.                    NULL_TREE, base_binfo,
  2155.                    basetypes);
  2156.       obstack_ptr_grow (&search_obstack, btypes);
  2157.       search_stack->first = (tree *)obstack_base (&search_obstack);
  2158.       tail += 1;
  2159.     }
  2160.  
  2161.     dont_queue:
  2162.       /* Process head of queue, if one exists.  */
  2163.       if (head >= tail)
  2164.     break;
  2165.  
  2166.       basetypes = search_stack->first[head++];
  2167.       binfo = TREE_VALUE (basetypes);
  2168.       type = BINFO_TYPE (binfo);
  2169.       index = lookup_fnfields_1 (type, name);
  2170.       if (index >= 0)
  2171.     {
  2172.       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  2173.       rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
  2174.       if (TYPE_BINFO_BASETYPES (type) == 0)
  2175.         goto dont_queue;
  2176.       else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
  2177.         {
  2178.           if (CLASSTYPE_BASELINK_VEC (type))
  2179.         TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  2180.           goto dont_queue;
  2181.         }
  2182.     }
  2183.       nval = NULL_TREE;
  2184.     }
  2185.  
  2186.   search_stack = pop_search_level (search_stack);
  2187.   return rval;
  2188. }
  2189.  
  2190. tree
  2191. next_baselink (baselink)
  2192.      tree baselink;
  2193. {
  2194.   tree tmp = TREE_TYPE (baselink);
  2195.   baselink = TREE_CHAIN (baselink);
  2196.   while (tmp)
  2197.     {
  2198.       /* @@ does not yet add previous base types.  */
  2199.       baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
  2200.                 baselink);
  2201.       TREE_TYPE (baselink) = TREE_TYPE (tmp);
  2202.       tmp = TREE_CHAIN (tmp);
  2203.     }
  2204.   return baselink;
  2205. }
  2206.  
  2207. /* DEPTH-FIRST SEARCH ROUTINES.  */
  2208.  
  2209. /* Assign unique numbers to _CLASSTYPE members of the lattice
  2210.    specified by TYPE.  The root nodes are marked first; the nodes
  2211.    are marked depth-fisrt, left-right.  */
  2212.  
  2213. static int cid;
  2214.  
  2215. /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
  2216.    Relation yields 1 if C1 <= C2, 0 otherwise.  */
  2217. typedef char mi_boolean;
  2218. static mi_boolean *mi_matrix;
  2219.  
  2220. /* Type for which this matrix is defined.  */
  2221. static tree mi_type;
  2222.  
  2223. /* Size of the matrix for indexing purposes.  */
  2224. static int mi_size;
  2225.  
  2226. /* Return nonzero if class C2 derives from class C1.  */
  2227. #define BINFO_DERIVES_FROM(C1, C2)    \
  2228.   ((mi_matrix+mi_size*(BINFO_CID (C1)-1))[BINFO_CID (C2)-1])
  2229. #define TYPE_DERIVES_FROM(C1, C2)    \
  2230.   ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
  2231. #define BINFO_DERIVES_FROM_STAR(C)    \
  2232.   (mi_matrix+(BINFO_CID (C)-1))
  2233.  
  2234. /* This routine converts a pointer to be a pointer of an immediate
  2235.    base class.  The normal convert_pointer_to routine would diagnose
  2236.    the conversion as ambiguous, under MI code that has the base class
  2237.    as an ambiguous base class. */
  2238. static tree
  2239. convert_pointer_to_single_level (to_type, expr)
  2240.      tree to_type, expr;
  2241. {
  2242.   tree binfo_of_derived;
  2243.   tree last;
  2244.  
  2245.   binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
  2246.   last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
  2247.   BINFO_INHERITANCE_CHAIN (last) = binfo_of_derived;
  2248.   BINFO_INHERITANCE_CHAIN (binfo_of_derived) = NULL_TREE;
  2249.   return build_vbase_path (PLUS_EXPR, TYPE_POINTER_TO (to_type), expr, last, 1);
  2250. }
  2251.  
  2252. /* The main function which implements depth first search.
  2253.  
  2254.    This routine has to remember the path it walked up, when
  2255.    dfs_init_vbase_pointers is the work function, as otherwise there
  2256.    would be no record. */
  2257. static void
  2258. dfs_walk (binfo, fn, qfn)
  2259.      tree binfo;
  2260.      void (*fn)();
  2261.      int (*qfn)();
  2262. {
  2263.   tree binfos = BINFO_BASETYPES (binfo);
  2264.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2265.  
  2266.   for (i = 0; i < n_baselinks; i++)
  2267.     {
  2268.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2269.  
  2270.       if (qfn == 0 || (*qfn)(base_binfo))
  2271.     {
  2272.       if (fn == dfs_init_vbase_pointers)
  2273.         {
  2274.           /* When traversing an arbitrary MI hierarchy, we need to keep
  2275.          a record of the path we took to get down to the final base
  2276.          type, as otherwise there would be no record of it, and just
  2277.          trying to blindly convert at the bottom would be ambiguous.
  2278.  
  2279.          The easiest way is to do the conversions one step at a time,
  2280.          as we know we want the immediate base class at each step.
  2281.  
  2282.          The only special trick to converting one step at a time,
  2283.          is that when we hit the last virtual base class, we must
  2284.          use the SLOT value for it, and not use the normal convert
  2285.          routine.  We use the last virtual base class, as in our
  2286.          implementation, we have pointers to all virtual base
  2287.          classes in the base object.  */
  2288.  
  2289.           tree saved_vbase_decl_ptr_intermediate
  2290.         = vbase_decl_ptr_intermediate;
  2291.  
  2292.           if (TREE_VIA_VIRTUAL (base_binfo))
  2293.         {
  2294.           /* No need for the conversion here, as we know it is the
  2295.              right type.  */
  2296.           vbase_decl_ptr_intermediate
  2297.             = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
  2298.         }
  2299.           else
  2300.         {
  2301.           vbase_decl_ptr_intermediate
  2302.             = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
  2303.                                vbase_decl_ptr_intermediate);
  2304.         }
  2305.  
  2306.           dfs_walk (base_binfo, fn, qfn);
  2307.  
  2308.           vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
  2309.         } else
  2310.           dfs_walk (base_binfo, fn, qfn);
  2311.     }
  2312.     }
  2313.  
  2314.   fn (binfo);
  2315. }
  2316.  
  2317. /* Predicate functions which serve for dfs_walk.  */
  2318. static int numberedp (binfo) tree binfo;
  2319. { return BINFO_CID (binfo); }
  2320. static int unnumberedp (binfo) tree binfo;
  2321. { return BINFO_CID (binfo) == 0; }
  2322.  
  2323. static int markedp (binfo) tree binfo;
  2324. { return BINFO_MARKED (binfo); }
  2325. static int bfs_markedp (binfo, i) tree binfo; int i;
  2326. { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
  2327. static int unmarkedp (binfo) tree binfo;
  2328. { return BINFO_MARKED (binfo) == 0; }
  2329. static int bfs_unmarkedp (binfo, i) tree binfo; int i;
  2330. { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2331. static int marked_vtable_pathp (binfo) tree binfo;
  2332. { return BINFO_VTABLE_PATH_MARKED (binfo); }
  2333. static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
  2334. { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
  2335. static int unmarked_vtable_pathp (binfo) tree binfo;
  2336. { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
  2337. static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
  2338. { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2339. static int marked_new_vtablep (binfo) tree binfo;
  2340. { return BINFO_NEW_VTABLE_MARKED (binfo); }
  2341. static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
  2342. { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
  2343. static int unmarked_new_vtablep (binfo) tree binfo;
  2344. { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
  2345. static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
  2346. { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2347.  
  2348. static int dfs_search_slot_nonempty_p (binfo) tree binfo;
  2349. { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
  2350.  
  2351. static int dfs_debug_unmarkedp (binfo) tree binfo;
  2352. { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
  2353.  
  2354. /* The worker functions for `dfs_walk'.  These do not need to
  2355.    test anything (vis a vis marking) if they are paired with
  2356.    a predicate function (above).  */
  2357.  
  2358. /* Assign each type within the lattice a number which is unique
  2359.    in the lattice.  The first number assigned is 1.  */
  2360.  
  2361. static void
  2362. dfs_number (binfo)
  2363.      tree binfo;
  2364. {
  2365.   BINFO_CID (binfo) = ++cid;
  2366. }
  2367.  
  2368. static void
  2369. dfs_unnumber (binfo)
  2370.      tree binfo;
  2371. {
  2372.   BINFO_CID (binfo) = 0;
  2373. }
  2374.  
  2375. static void
  2376. dfs_mark (binfo) tree binfo;
  2377. { SET_BINFO_MARKED (binfo); }
  2378.  
  2379. static void
  2380. dfs_unmark (binfo) tree binfo;
  2381. { CLEAR_BINFO_MARKED (binfo); }
  2382.  
  2383. static void
  2384. dfs_mark_vtable_path (binfo) tree binfo;
  2385. { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
  2386.  
  2387. static void
  2388. dfs_unmark_vtable_path (binfo) tree binfo;
  2389. { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
  2390.  
  2391. static void
  2392. dfs_mark_new_vtable (binfo) tree binfo;
  2393. { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
  2394.  
  2395. static void
  2396. dfs_unmark_new_vtable (binfo) tree binfo;
  2397. { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
  2398.  
  2399. static void
  2400. dfs_clear_search_slot (binfo) tree binfo;
  2401. { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
  2402.  
  2403. static void
  2404. dfs_debug_mark (binfo)
  2405.      tree binfo;
  2406. {
  2407.   tree t = BINFO_TYPE (binfo);
  2408.  
  2409.   /* Use heuristic that if there are virtual functions,
  2410.      ignore until we see a non-inline virtual function.  */
  2411.   tree methods = CLASSTYPE_METHOD_VEC (t);
  2412.  
  2413.   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
  2414.  
  2415.   /* If interface info is known, the value of (?@@?) is correct.  */
  2416.   if (methods == 0
  2417.       || CLASSTYPE_INTERFACE_KNOWN (t)
  2418.       || (write_virtuals == 2 && TYPE_VIRTUAL_P (t)))
  2419.     return;
  2420.  
  2421.   /* If debug info is requested from this context for this type, supply it.
  2422.      If debug info is requested from another context for this type,
  2423.      see if some third context can supply it.  */
  2424.   if (current_function_decl == NULL_TREE
  2425.       || DECL_CLASS_CONTEXT (current_function_decl) != t)
  2426.     {
  2427.       if (TREE_VEC_ELT (methods, 0))
  2428.     methods = TREE_VEC_ELT (methods, 0);
  2429.       else
  2430.     methods = TREE_VEC_ELT (methods, 1);
  2431.       while (methods)
  2432.     {
  2433.       if (DECL_VINDEX (methods)
  2434.           && DECL_THIS_INLINE (methods) == 0
  2435.           && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
  2436.         {
  2437.           /* Somebody, somewhere is going to have to define this
  2438.          virtual function.  When they do, they will provide
  2439.          the debugging info.  */
  2440.           return;
  2441.         }
  2442.       methods = TREE_CHAIN (methods);
  2443.     }
  2444.     }
  2445.   /* We cannot rely on some alien method to solve our problems,
  2446.      so we must write out the debug info ourselves.  */
  2447.   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
  2448.   rest_of_type_compilation (t, global_bindings_p ());
  2449. }
  2450.  
  2451. /*  Attach to the type of the virtual base class, the pointer to the
  2452.     virtual base class, given the global pointer vbase_decl_ptr.
  2453.  
  2454.     We use the global vbase_types.  ICK!  */
  2455. static void
  2456. dfs_find_vbases (binfo)
  2457.      tree binfo;
  2458. {
  2459.   tree binfos = BINFO_BASETYPES (binfo);
  2460.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2461.  
  2462.   for (i = n_baselinks-1; i >= 0; i--)
  2463.     {
  2464.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2465.  
  2466.       if (TREE_VIA_VIRTUAL (base_binfo)
  2467.       && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
  2468.     {
  2469.       tree vbase = BINFO_TYPE (base_binfo);
  2470.       tree binfo = binfo_member (vbase, vbase_types);
  2471.  
  2472.       CLASSTYPE_SEARCH_SLOT (vbase)
  2473.         = (char *) build (PLUS_EXPR, TYPE_POINTER_TO (vbase),
  2474.                   vbase_decl_ptr, BINFO_OFFSET (binfo));
  2475.     }
  2476.     }
  2477.   SET_BINFO_VTABLE_PATH_MARKED (binfo);
  2478.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  2479. }
  2480.  
  2481. static void
  2482. dfs_init_vbase_pointers (binfo)
  2483.      tree binfo;
  2484. {
  2485.   tree type = BINFO_TYPE (binfo);
  2486.   tree fields = TYPE_FIELDS (type);
  2487.   tree this_vbase_ptr;
  2488.  
  2489.   CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
  2490.  
  2491.   /* If there is a rtti, it is the first field, though perhaps from
  2492.      the base class.  Otherwise, the first fields are virtual base class
  2493.      pointer fields.  */
  2494.   if (CLASSTYPE_RTTI (type) && VFIELD_NAME_P (DECL_NAME (fields)))
  2495.     /* Get past vtable for the object.  */
  2496.     fields = TREE_CHAIN (fields);
  2497.  
  2498.   if (fields == NULL_TREE
  2499.       || DECL_NAME (fields) == NULL_TREE
  2500.       || ! VBASE_NAME_P (DECL_NAME (fields)))
  2501.     return;
  2502.  
  2503.   this_vbase_ptr = vbase_decl_ptr_intermediate;
  2504.  
  2505.   if (TYPE_POINTER_TO (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
  2506.     my_friendly_abort (125);
  2507.  
  2508.   while (fields && DECL_NAME (fields)
  2509.      && VBASE_NAME_P (DECL_NAME (fields)))
  2510.     {
  2511.       tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
  2512.             build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
  2513.       tree init = (tree)CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
  2514.       vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
  2515.                            vbase_types),
  2516.                      build_modify_expr (ref, NOP_EXPR, init),
  2517.                      vbase_init_result);
  2518.       fields = TREE_CHAIN (fields);
  2519.     }
  2520. }
  2521.  
  2522. /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE.  Other
  2523.    times, just NEW_VTABLE, but optimizer should make both with equal
  2524.    efficiency (though it does not currently).  */
  2525. static void
  2526. dfs_clear_vbase_slots (binfo)
  2527.      tree binfo;
  2528. {
  2529.   tree type = BINFO_TYPE (binfo);
  2530.   CLASSTYPE_SEARCH_SLOT (type) = 0;
  2531.   CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
  2532.   CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
  2533. }
  2534.  
  2535. tree
  2536. init_vbase_pointers (type, decl_ptr)
  2537.      tree type;
  2538.      tree decl_ptr;
  2539. {
  2540.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2541.     {
  2542.       int old_flag = flag_this_is_variable;
  2543.       tree binfo = TYPE_BINFO (type);
  2544.       flag_this_is_variable = -2;
  2545.       vbase_types = CLASSTYPE_VBASECLASSES (type);
  2546.       vbase_decl_ptr = decl_ptr;
  2547.       vbase_decl = build_indirect_ref (decl_ptr, NULL_PTR);
  2548.       vbase_decl_ptr_intermediate = vbase_decl_ptr;
  2549.       vbase_init_result = NULL_TREE;
  2550.       dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
  2551.       dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
  2552.       dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
  2553.       flag_this_is_variable = old_flag;
  2554.       return vbase_init_result;
  2555.     }
  2556.   return 0;
  2557. }
  2558.  
  2559. /* get the virtual context (the vbase that directly contains the
  2560.    DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
  2561.    or NULL_TREE if there is none.
  2562.  
  2563.    FNDECL must come from a virtual table from a virtual base to ensure that
  2564.    there is only one possible DECL_CLASS_CONTEXT.
  2565.  
  2566.    We know that if there is more than one place (binfo) the fndecl that the
  2567.    declared, they all refer to the same binfo.  See get_class_offset_1 for
  2568.    the check that ensures this.  */
  2569. static tree
  2570. virtual_context (fndecl, t, vbase)
  2571.      tree fndecl, t, vbase;
  2572. {
  2573.   tree path;
  2574.   if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
  2575.     {
  2576.       /* This shouldn't happen, I don't want errors! */
  2577.       warning ("recoverable compiler error, fixups for virtual function");
  2578.       return vbase;
  2579.     }
  2580.   while (path)
  2581.     {
  2582.       if (TREE_VIA_VIRTUAL (path))
  2583.     return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
  2584.       path = BINFO_INHERITANCE_CHAIN (path);
  2585.     }
  2586.   return 0;
  2587. }
  2588.  
  2589. /* Fixups upcast offsets for one vtable.
  2590.    Entries may stay within the VBASE given, or
  2591.    they may upcast into a direct base, or
  2592.    they may upcast into a different vbase.
  2593.  
  2594.    We only need to do fixups in case 2 and 3.
  2595.  
  2596.    This routine mirrors fixup_vtable_deltas in functionality, though
  2597.    this one is runtime based, and the other is compile time based.
  2598.    Conceivably that routine could be removed entirely, and all fixups
  2599.    done at runtime.
  2600.  
  2601.    VBASE_OFFSETS is an association list of virtual bases that contains
  2602.    offset information, so the offsets are only calculated once.  */
  2603. static void
  2604. expand_upcast_fixups (binfo, addr, orig_addr, vbase, t, vbase_offsets)
  2605.      tree binfo, addr, orig_addr, vbase, t, *vbase_offsets;
  2606. {
  2607.   tree virtuals = BINFO_VIRTUALS (binfo);
  2608.   tree vc;
  2609.   tree delta;
  2610.   unsigned HOST_WIDE_INT n;
  2611.   
  2612.   delta = purpose_member (vbase, *vbase_offsets);
  2613.   if (! delta)
  2614.     {
  2615.       delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
  2616.       delta = build (MINUS_EXPR, ptrdiff_type_node, delta, addr);
  2617.       delta = save_expr (delta);
  2618.       delta = tree_cons (vbase, delta, *vbase_offsets);
  2619.       *vbase_offsets = delta;
  2620.     }
  2621.  
  2622.   /* Skip RTTI fake object. */
  2623.   n = 1;
  2624.   if (virtuals)
  2625.     virtuals = TREE_CHAIN (virtuals);
  2626.   while (virtuals)
  2627.     {
  2628.       tree current_fndecl = TREE_VALUE (virtuals);
  2629.       current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
  2630.       current_fndecl = TREE_OPERAND (current_fndecl, 0);
  2631.       if (current_fndecl
  2632.       && current_fndecl != abort_fndecl
  2633.       && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
  2634.     {
  2635.       /* This may in fact need a runtime fixup. */
  2636.       tree idx = DECL_VINDEX (current_fndecl);
  2637.       tree vtbl = BINFO_VTABLE (binfo);
  2638.       tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
  2639.       tree aref, ref, naref;
  2640.       tree old_delta, new_delta;
  2641.       tree init;
  2642.  
  2643.       if (nvtbl == NULL_TREE
  2644.           || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
  2645.         {
  2646.           /* Dup it if it isn't in local scope yet.  */
  2647.           nvtbl = build_decl (VAR_DECL,
  2648.                   DECL_NAME (vtbl),
  2649.                   TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo))));
  2650.           DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
  2651.                     DECL_ALIGN (nvtbl));
  2652.           TREE_READONLY (nvtbl) = 0;
  2653.           nvtbl = pushdecl (nvtbl);
  2654.           init = NULL_TREE;
  2655.           cp_finish_decl (nvtbl, init, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
  2656.           DECL_VIRTUAL_P (nvtbl) = 1;
  2657.           DECL_CONTEXT (nvtbl) = t;
  2658.           init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
  2659.                 nvtbl, vtbl);
  2660.           TREE_SIDE_EFFECTS (init) = 1;
  2661.           expand_expr_stmt (init);
  2662.           /* Update the vtable pointers as necessary. */
  2663.           ref = build_vfield_ref (build_indirect_ref (addr, NULL_PTR), DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
  2664.           expand_expr_stmt (build_modify_expr (ref, NOP_EXPR,
  2665.                            build_unary_op (ADDR_EXPR, nvtbl, 0)));
  2666.         }
  2667.       assemble_external (vtbl);
  2668.       aref = build_array_ref (vtbl, idx);
  2669.       naref = build_array_ref (nvtbl, idx);
  2670.       old_delta = build_component_ref (aref, delta_identifier, 0, 0);
  2671.       new_delta = build_component_ref (naref, delta_identifier, 0, 0);
  2672.       old_delta = build_binary_op (PLUS_EXPR, old_delta,
  2673.                        TREE_VALUE (delta), 0);
  2674.       if (vc)
  2675.         {
  2676.           /* If this is set, we need to add in delta adjustments for
  2677.          the other virtual base.  */
  2678.           tree vc_delta = purpose_member (vc, *vbase_offsets);
  2679.           if (! vc_delta)
  2680.         {
  2681.           tree vc_addr = convert_pointer_to_real (vc, orig_addr);
  2682.           vc_delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
  2683.           vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
  2684.                     vc_addr, vc_delta);
  2685.           vc_delta = save_expr (vc_delta);
  2686.           *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
  2687.         }
  2688.           else
  2689.         vc_delta = TREE_VALUE (vc_delta);
  2690.    
  2691.           old_delta = build_binary_op (PLUS_EXPR, old_delta, vc_delta, 0);
  2692.         }
  2693.  
  2694.       TREE_READONLY (new_delta) = 0;
  2695.       expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
  2696.                            old_delta));
  2697.     }
  2698.       ++n;
  2699.       virtuals = TREE_CHAIN (virtuals);
  2700.     }
  2701. }
  2702.  
  2703. /* Fixup upcast offsets for all direct vtables.  Patterned after
  2704.    expand_direct_vtbls_init.  */
  2705. static void
  2706. fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
  2707.      tree real_binfo, binfo, addr, orig_addr, type, vbase, *vbase_offsets;
  2708.      int init_self, can_elide;
  2709. {
  2710.   tree real_binfos = BINFO_BASETYPES (real_binfo);
  2711.   tree binfos = BINFO_BASETYPES (binfo);
  2712.   int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
  2713.  
  2714.   for (i = 0; i < n_baselinks; i++)
  2715.     {
  2716.       tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
  2717.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2718.       int is_not_base_vtable =
  2719.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
  2720.       if (! TREE_VIA_VIRTUAL (real_base_binfo))
  2721.     fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
  2722.                       is_not_base_vtable, can_elide, addr,
  2723.                       orig_addr, type, vbase, vbase_offsets);
  2724.     }
  2725. #if 0
  2726.   /* Before turning this on, make sure it is correct.  */
  2727.   if (can_elide && ! BINFO_MODIFIED (binfo))
  2728.     return;
  2729. #endif
  2730.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2731.   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
  2732.     {
  2733.       addr = convert_pointer_to_real (binfo, addr);
  2734.       expand_upcast_fixups (real_binfo, addr, orig_addr, vbase, type, vbase_offsets);
  2735.     }
  2736. }
  2737.  
  2738. /* Build a COMPOUND_EXPR which when expanded will generate the code
  2739.    needed to initialize all the virtual function table slots of all
  2740.    the virtual baseclasses.  MAIN_BINFO is the binfo which determines
  2741.    the virtual baseclasses to use; TYPE is the type of the object to
  2742.    which the initialization applies.  TRUE_EXP is the true object we
  2743.    are initializing, and DECL_PTR is the pointer to the sub-object we
  2744.    are initializing.
  2745.  
  2746.    When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
  2747.    object was laid out by a top-level constructor and the computed
  2748.    offsets are valid to store vtables.  When zero, we must store new
  2749.    vtables through virtual baseclass pointers.
  2750.  
  2751.    We setup and use the globals: vbase_decl, vbase_decl_ptr, vbase_types
  2752.    ICK!  */
  2753.  
  2754. void
  2755. expand_indirect_vtbls_init (binfo, true_exp, decl_ptr, use_computed_offsets)
  2756.      tree binfo;
  2757.      tree true_exp, decl_ptr;
  2758.      int use_computed_offsets;
  2759. {
  2760.   tree type = BINFO_TYPE (binfo);
  2761.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2762.     {
  2763.       rtx fixup_insns = NULL_RTX;
  2764.       int old_flag = flag_this_is_variable;
  2765.       tree vbases = CLASSTYPE_VBASECLASSES (type);
  2766.       vbase_types = vbases;
  2767.       vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
  2768.       vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, NULL_PTR);
  2769.  
  2770.       if (use_computed_offsets)
  2771.     {
  2772.       /* This is an object of type IN_TYPE,  */
  2773.       flag_this_is_variable = -2;
  2774.     }
  2775.  
  2776.       dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
  2777.  
  2778.       /* Initialized with vtables of type TYPE.  */
  2779.       for (; vbases; vbases = TREE_CHAIN (vbases))
  2780.     {
  2781.       tree addr;
  2782.       if (use_computed_offsets)
  2783.         addr = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbases));
  2784.       else
  2785.         {
  2786.           tree vbinfo = get_binfo (TREE_TYPE (vbases),
  2787.                        TREE_TYPE (vbase_decl),
  2788.                        0);
  2789.  
  2790.           /* See is we can get lucky.  */
  2791.           if (TREE_VIA_VIRTUAL (vbinfo))
  2792.         addr = convert_pointer_to_real (vbinfo, vbase_decl_ptr);
  2793.           else
  2794.         {
  2795.           /* We go through all these contortions to avoid this
  2796.              call, as it will fail when the virtual base type
  2797.              is ambiguous from here.  We don't yet have a way
  2798.              to search for and find just an instance of the
  2799.              virtual base class.  Searching for the binfo in
  2800.              vbases won't work, as we don't have the vbase
  2801.              pointer field, for all vbases in the main class,
  2802.              only direct vbases.  */
  2803.           addr = convert_pointer_to_real (TREE_TYPE (vbases),
  2804.                           vbase_decl_ptr);
  2805.           if (addr == error_mark_node)
  2806.             continue;
  2807.         }
  2808.         }
  2809.  
  2810.       /* Do all vtables from this virtual base. */
  2811.       /* This assumes that virtual bases can never serve as parent
  2812.          binfos.  (in the CLASSTPE_VFIELD_PARENT sense)  */
  2813.       expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
  2814.                     1, 0, addr);
  2815.  
  2816.       /* If we are using computed offsets we can skip fixups.  */
  2817.       if (use_computed_offsets)
  2818.         continue;
  2819.  
  2820.       /* Now we adjust the offsets for virtual functions that cross
  2821.          virtual boundaries on an implicit upcast on vf call so that
  2822.          the layout of the most complete type is used, instead of
  2823.          assuming the layout of the virtual bases from our current type. */
  2824.  
  2825.       if (flag_vtable_thunks)
  2826.         {
  2827.           /* We don't have dynamic thunks yet!  So for now, just fail silently. */
  2828.         }
  2829.       else
  2830.         {
  2831.           tree vbase_offsets = NULL_TREE;
  2832.           push_to_sequence (fixup_insns);
  2833.           fixup_virtual_upcast_offsets (vbases,
  2834.                         TYPE_BINFO (BINFO_TYPE (vbases)),
  2835.                         1, 0, addr, vbase_decl_ptr,
  2836.                         type, vbases, &vbase_offsets);
  2837.           fixup_insns = get_insns ();
  2838.           end_sequence ();
  2839.         }
  2840.     }
  2841.  
  2842.       if (fixup_insns)
  2843.     {
  2844.       extern tree in_charge_identifier;
  2845.       tree in_charge_node = lookup_name (in_charge_identifier, 0);
  2846.       if (! in_charge_node)
  2847.         {
  2848.           warning ("recoverable internal compiler error, nobody's in charge!");
  2849.           in_charge_node = integer_zero_node;
  2850.         }
  2851.       in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
  2852.       expand_start_cond (in_charge_node, 0);
  2853.       emit_insns (fixup_insns);
  2854.       expand_end_cond ();
  2855.     }
  2856.  
  2857.       dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
  2858.  
  2859.       flag_this_is_variable = old_flag;
  2860.     }
  2861. }
  2862.  
  2863. void
  2864. clear_search_slots (type)
  2865.      tree type;
  2866. {
  2867.   dfs_walk (TYPE_BINFO (type),
  2868.         dfs_clear_search_slot, dfs_search_slot_nonempty_p);
  2869. }
  2870.  
  2871. /* get virtual base class types.
  2872.    This adds type to the vbase_types list in reverse dfs order.
  2873.    Ordering is very important, so don't change it.  */
  2874.  
  2875. static void
  2876. dfs_get_vbase_types (binfo)
  2877.      tree binfo;
  2878. {
  2879.   if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
  2880.     {
  2881.       vbase_types = make_binfo (integer_zero_node, binfo,
  2882.                 BINFO_VTABLE (binfo),
  2883.                 BINFO_VIRTUALS (binfo), vbase_types);
  2884.       TREE_VIA_VIRTUAL (vbase_types) = 1;
  2885.       SET_BINFO_VBASE_MARKED (binfo);
  2886.     }
  2887.   SET_BINFO_MARKED (binfo);
  2888. }
  2889.  
  2890. /* get a list of virtual base classes in dfs order.  */
  2891. tree
  2892. get_vbase_types (type)
  2893.      tree type;
  2894. {
  2895.   tree vbases;
  2896.   tree binfo;
  2897.  
  2898.   if (TREE_CODE (type) == TREE_VEC)
  2899.     binfo = type;
  2900.   else
  2901.     binfo = TYPE_BINFO (type);
  2902.  
  2903.   vbase_types = NULL_TREE;
  2904.   dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
  2905.   dfs_walk (binfo, dfs_unmark, markedp);
  2906.   /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
  2907.      reverse it so that we get normal dfs ordering.  */
  2908.   vbase_types = nreverse (vbase_types);
  2909.  
  2910.   /* unmark marked vbases */
  2911.   for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
  2912.     CLEAR_BINFO_VBASE_MARKED (vbases);
  2913.  
  2914.   return vbase_types;
  2915. }
  2916.  
  2917. static void
  2918. dfs_record_inheritance (binfo)
  2919.      tree binfo;
  2920. {
  2921.   tree binfos = BINFO_BASETYPES (binfo);
  2922.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2923.   mi_boolean *derived_row = BINFO_DERIVES_FROM_STAR (binfo);
  2924.  
  2925.   for (i = n_baselinks-1; i >= 0; i--)
  2926.     {
  2927.       int j;
  2928.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2929.       tree baseclass = BINFO_TYPE (base_binfo);
  2930.       mi_boolean *base_row = BINFO_DERIVES_FROM_STAR (base_binfo);
  2931.  
  2932.       /* Don't search if there's nothing there!  MI_SIZE can be
  2933.      zero as a result of parse errors.  */
  2934.       if (TYPE_BINFO_BASETYPES (baseclass) && mi_size > 0)
  2935.     for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
  2936.       derived_row[j] |= base_row[j];
  2937.       TYPE_DERIVES_FROM (baseclass, BINFO_TYPE (binfo)) = 1;
  2938.     }
  2939.  
  2940.   SET_BINFO_MARKED (binfo);
  2941. }
  2942.  
  2943. /* Given a _CLASSTYPE node in a multiple inheritance lattice,
  2944.    convert the lattice into a simple relation such that,
  2945.    given to CIDs, C1 and C2, one can determine if C1 <= C2
  2946.    or C2 <= C1 or C1 <> C2.
  2947.  
  2948.    Once constructed, we walk the lattice depth fisrt,
  2949.    applying various functions to elements as they are encountered.
  2950.  
  2951.    We use xmalloc here, in case we want to randomly free these tables.  */
  2952.  
  2953. #define SAVE_MI_MATRIX
  2954.  
  2955. void
  2956. build_mi_matrix (type)
  2957.      tree type;
  2958. {
  2959.   tree binfo = TYPE_BINFO (type);
  2960.   cid = 0;
  2961.  
  2962. #ifdef SAVE_MI_MATRIX
  2963.   if (CLASSTYPE_MI_MATRIX (type))
  2964.     {
  2965.       mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2966.       mi_matrix = CLASSTYPE_MI_MATRIX (type);
  2967.       mi_type = type;
  2968.       dfs_walk (binfo, dfs_number, unnumberedp);
  2969.       return;
  2970.     }
  2971. #endif
  2972.  
  2973.   mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2974.   mi_matrix = (char *)xmalloc ((mi_size + 1) * (mi_size + 1));
  2975.   mi_type = type;
  2976.   bzero (mi_matrix, (mi_size + 1) * (mi_size + 1));
  2977.   dfs_walk (binfo, dfs_number, unnumberedp);
  2978.   dfs_walk (binfo, dfs_record_inheritance, unmarkedp);
  2979.   dfs_walk (binfo, dfs_unmark, markedp);
  2980. }
  2981.  
  2982. void
  2983. free_mi_matrix ()
  2984. {
  2985.   dfs_walk (TYPE_BINFO (mi_type), dfs_unnumber, numberedp);
  2986.  
  2987. #ifdef SAVE_MI_MATRIX
  2988.   CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
  2989. #else
  2990.   free (mi_matrix);
  2991.   mi_size = 0;
  2992.   cid = 0;
  2993. #endif
  2994. }
  2995.  
  2996. /* If we want debug info for a type TYPE, make sure all its base types
  2997.    are also marked as being potentially interesting.  This avoids
  2998.    the problem of not writing any debug info for intermediate basetypes
  2999.    that have abstract virtual functions.  Also mark member types.  */
  3000.  
  3001. void
  3002. note_debug_info_needed (type)
  3003.      tree type;
  3004. {
  3005.   tree field;
  3006.   dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
  3007.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  3008.     {
  3009.       tree ttype;
  3010.       if (TREE_CODE (field) == FIELD_DECL
  3011.       && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
  3012.       && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
  3013.     note_debug_info_needed (ttype);
  3014.     }
  3015. }
  3016.  
  3017. /* Subroutines of push_class_decls ().  */
  3018.  
  3019. /* Add the instance variables which this class contributed to the
  3020.    current class binding contour.  When a redefinition occurs,
  3021.    if the redefinition is strictly within a single inheritance path,
  3022.    we just overwrite (in the case of a data field) or
  3023.    cons (in the case of a member function) the old declaration with
  3024.    the new.  If the fields are not within a single inheritance path,
  3025.    we must cons them in either case.
  3026.  
  3027.    In order to know what decls are new (stemming from the current
  3028.    invocation of push_class_decls) we enclose them in an "envelope",
  3029.    which is a TREE_LIST node where the TREE_PURPOSE slot contains the
  3030.    new decl (or possibly a list of competing ones), the TREE_VALUE slot
  3031.    points to the old value and the TREE_CHAIN slot chains together all
  3032.    envelopes which needs to be "opened" in push_class_decls.  Opening an
  3033.    envelope means: push the old value onto the class_shadowed list,
  3034.    install the new one and if it's a TYPE_DECL do the same to the
  3035.    IDENTIFIER_TYPE_VALUE.  Such an envelope is recognized by seeing that
  3036.    the TREE_PURPOSE slot is non-null, and that it is not an identifier.
  3037.    Because if it is, it could be a set of overloaded methods from an
  3038.    outer scope.  */
  3039.  
  3040. static void
  3041. dfs_pushdecls (binfo)
  3042.      tree binfo;
  3043. {
  3044.   tree type = BINFO_TYPE (binfo);
  3045.   tree fields, *methods, *end;
  3046.   tree method_vec;
  3047.  
  3048.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  3049.     {
  3050.       /* Unmark so that if we are in a constructor, and then find that
  3051.      this field was initialized by a base initializer,
  3052.      we can emit an error message.  */
  3053.       if (TREE_CODE (fields) == FIELD_DECL)
  3054.     TREE_USED (fields) = 0;
  3055.  
  3056.       /* Recurse into anonymous unions.  */
  3057.       if (DECL_NAME (fields) == NULL_TREE
  3058.       && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
  3059.     {
  3060.       dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
  3061.       continue;
  3062.     }
  3063.  
  3064. #if 0
  3065.       if (TREE_CODE (fields) != TYPE_DECL)
  3066.     {
  3067.       DECL_PUBLIC (fields) = 0;
  3068.       DECL_PROTECTED (fields) = 0;
  3069.       DECL_PRIVATE (fields) = 0;
  3070.     }
  3071. #endif
  3072.  
  3073.       if (DECL_NAME (fields))
  3074.     {
  3075.       tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (fields));
  3076.  
  3077.       /* If the class value is an envelope of the kind described in
  3078.          the comment above, we try to rule out possible ambiguities.
  3079.          If we can't do that, keep a TREE_LIST with possibly ambiguous
  3080.          decls in there.  */
  3081.       if (class_value && TREE_CODE (class_value) == TREE_LIST
  3082.           && TREE_PURPOSE (class_value) != NULL_TREE
  3083.           && (TREE_CODE (TREE_PURPOSE (class_value))
  3084.           != IDENTIFIER_NODE))
  3085.         {
  3086.           tree value = TREE_PURPOSE (class_value);
  3087.           tree context;
  3088.  
  3089.           /* Possible ambiguity.  If its defining type(s)
  3090.          is (are all) derived from us, no problem.  */
  3091.           if (TREE_CODE (value) != TREE_LIST)
  3092.         {
  3093.           context = (TREE_CODE (value) == FUNCTION_DECL
  3094.                  && DECL_VIRTUAL_P (value))
  3095.             ? DECL_CLASS_CONTEXT (value)
  3096.               : DECL_CONTEXT (value);
  3097.  
  3098.           if (context == type)
  3099.             {
  3100.               if (TREE_CODE (value) == TYPE_DECL
  3101.               && DECL_ARTIFICIAL (value))
  3102.             value = fields;
  3103.               /* else the old value wins */
  3104.             }
  3105.           else if (context && TYPE_DERIVES_FROM (context, type))
  3106.             value = fields;
  3107.           else
  3108.             value = tree_cons (NULL_TREE, fields,
  3109.                        build_tree_list (NULL_TREE, value));
  3110.         }
  3111.           else
  3112.         {
  3113.           /* All children may derive from us, in which case
  3114.              there is no problem.  Otherwise, we have to
  3115.              keep lists around of what the ambiguities might be.  */
  3116.           tree values;
  3117.           int problem = 0;
  3118.  
  3119.           for (values = value; values; values = TREE_CHAIN (values))
  3120.             {
  3121.               tree sub_values = TREE_VALUE (values);
  3122.  
  3123.               if (TREE_CODE (sub_values) == TREE_LIST)
  3124.             {
  3125.               for (; sub_values; sub_values = TREE_CHAIN (sub_values))
  3126.                 {
  3127.                   register tree list_mbr = TREE_VALUE (sub_values);
  3128.  
  3129.                   context = (TREE_CODE (list_mbr) == FUNCTION_DECL
  3130.                      && DECL_VIRTUAL_P (list_mbr))
  3131.                 ? DECL_CLASS_CONTEXT (list_mbr)
  3132.                   : DECL_CONTEXT (list_mbr);
  3133.  
  3134.                   if (! TYPE_DERIVES_FROM (context, type))
  3135.                 {
  3136.                   value = tree_cons (NULL_TREE, TREE_VALUE (values), value);
  3137.                   problem = 1;
  3138.                   break;
  3139.                 }
  3140.                 }
  3141.             }
  3142.               else
  3143.             {
  3144.               context = (TREE_CODE (sub_values) == FUNCTION_DECL
  3145.                      && DECL_VIRTUAL_P (sub_values))
  3146.                 ? DECL_CLASS_CONTEXT (sub_values)
  3147.                   : DECL_CONTEXT (sub_values);
  3148.  
  3149.               if (context && ! TYPE_DERIVES_FROM (context, type))
  3150.                 {
  3151.                   value = tree_cons (NULL_TREE, values, value);
  3152.                   problem = 1;
  3153.                   break;
  3154.                 }
  3155.             }
  3156.             }
  3157.           if (! problem) value = fields;
  3158.         }
  3159.  
  3160.           /* Mark this as a potentially ambiguous member.  */
  3161.           if (TREE_CODE (value) == TREE_LIST)
  3162.         {
  3163.           /* Leaving TREE_TYPE blank is intentional.
  3164.              We cannot use `error_mark_node' (lookup_name)
  3165.              or `unknown_type_node' (all member functions use this).  */
  3166.           TREE_NONLOCAL_FLAG (value) = 1;
  3167.         }
  3168.  
  3169.           /* Put the new contents in our envelope.  */
  3170.           TREE_PURPOSE (class_value) = value;
  3171.         }
  3172.       else
  3173.         {
  3174.           /* See comment above for a description of envelopes.  */
  3175.           tree envelope = tree_cons (fields, class_value,
  3176.                      closed_envelopes);
  3177.  
  3178.           closed_envelopes = envelope;
  3179.           IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = envelope;
  3180.         }
  3181.     }
  3182.     }
  3183.  
  3184.   method_vec = CLASSTYPE_METHOD_VEC (type);
  3185.   if (method_vec != 0)
  3186.     {
  3187.       /* Farm out constructors and destructors.  */
  3188.       methods = &TREE_VEC_ELT (method_vec, 1);
  3189.       end = TREE_VEC_END (method_vec);
  3190.  
  3191.       /* This does not work for multiple inheritance yet.  */
  3192.       while (methods != end)
  3193.     {
  3194.       /* This will cause lookup_name to return a pointer
  3195.          to the tree_list of possible methods of this name.
  3196.          If the order is a problem, we can nreverse them.  */
  3197.       tree tmp;
  3198.       tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
  3199.  
  3200.       if (class_value && TREE_CODE (class_value) == TREE_LIST
  3201.           && TREE_PURPOSE (class_value) != NULL_TREE
  3202.           && TREE_CODE (TREE_PURPOSE (class_value)) != IDENTIFIER_NODE)
  3203.         {
  3204.           tree old = TREE_PURPOSE (class_value);
  3205.  
  3206.           maybe_push_cache_obstack ();
  3207.           if (TREE_CODE (old) == TREE_LIST)
  3208.         tmp = tree_cons (DECL_NAME (*methods), *methods, old);
  3209.           else
  3210.         {
  3211.           /* Only complain if we shadow something we can access.  */
  3212.           if (old
  3213.               && warn_shadow
  3214.               && ((DECL_LANG_SPECIFIC (old)
  3215.                && DECL_CLASS_CONTEXT (old) == current_class_type)
  3216.               || ! TREE_PRIVATE (old)))
  3217.             /* Should figure out access control more accurately.  */
  3218.             {
  3219.               cp_warning_at ("member `%#D' is shadowed", old);
  3220.               cp_warning_at ("by member function `%#D'", *methods);
  3221.               warning ("in this context");
  3222.             }
  3223.           tmp = build_tree_list (DECL_NAME (*methods), *methods);
  3224.         }
  3225.           pop_obstacks ();
  3226.  
  3227.           TREE_TYPE (tmp) = unknown_type_node;
  3228. #if 0
  3229.           TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
  3230. #endif
  3231.           TREE_NONLOCAL_FLAG (tmp) = 1;
  3232.           
  3233.           /* Put the new contents in our envelope.  */
  3234.           TREE_PURPOSE (class_value) = tmp;
  3235.         }
  3236.       else
  3237.         {
  3238.           maybe_push_cache_obstack ();
  3239.           tmp = build_tree_list (DECL_NAME (*methods), *methods);
  3240.           pop_obstacks ();
  3241.  
  3242.           TREE_TYPE (tmp) = unknown_type_node;
  3243. #if 0
  3244.           TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
  3245. #endif
  3246.           TREE_NONLOCAL_FLAG (tmp) = 1;
  3247.           
  3248.           /* See comment above for a description of envelopes.  */
  3249.           closed_envelopes = tree_cons (tmp, class_value,
  3250.                         closed_envelopes);
  3251.           IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods)) = closed_envelopes;
  3252.         }
  3253. #if 0
  3254.       tmp = *methods;
  3255.       while (tmp != 0)
  3256.         {
  3257.           DECL_PUBLIC (tmp) = 0;
  3258.           DECL_PROTECTED (tmp) = 0;
  3259.           DECL_PRIVATE (tmp) = 0;
  3260.           tmp = DECL_CHAIN (tmp);
  3261.         }
  3262. #endif
  3263.  
  3264.       methods++;
  3265.     }
  3266.     }
  3267.   SET_BINFO_MARKED (binfo);
  3268. }
  3269.  
  3270. /* Consolidate unique (by name) member functions.  */
  3271. static void
  3272. dfs_compress_decls (binfo)
  3273.      tree binfo;
  3274. {
  3275.   tree type = BINFO_TYPE (binfo);
  3276.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  3277.  
  3278.   if (method_vec != 0)
  3279.     {
  3280.       /* Farm out constructors and destructors.  */
  3281.       tree *methods = &TREE_VEC_ELT (method_vec, 1);
  3282.       tree *end = TREE_VEC_END (method_vec);
  3283.  
  3284.       for (; methods != end; methods++)
  3285.     {
  3286.       /* This is known to be an envelope of the kind described before
  3287.          dfs_pushdecls.  */
  3288.       tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
  3289.       tree tmp = TREE_PURPOSE (class_value);
  3290.  
  3291.       /* This was replaced in scope by somebody else.  Just leave it
  3292.          alone.  */
  3293.       if (TREE_CODE (tmp) != TREE_LIST)
  3294.         continue;
  3295.  
  3296.       if (TREE_CHAIN (tmp) == NULL_TREE
  3297.           && TREE_VALUE (tmp)
  3298.           && DECL_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
  3299.         {
  3300.           TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
  3301.         }
  3302.     }
  3303.     }
  3304.   CLEAR_BINFO_MARKED (binfo);
  3305. }
  3306.  
  3307. /* When entering the scope of a class, we cache all of the
  3308.    fields that that class provides within its inheritance
  3309.    lattice.  Where ambiguities result, we mark them
  3310.    with `error_mark_node' so that if they are encountered
  3311.    without explicit qualification, we can emit an error
  3312.    message.  */
  3313. void
  3314. push_class_decls (type)
  3315.      tree type;
  3316. {
  3317.   tree id;
  3318.   struct obstack *ambient_obstack = current_obstack;
  3319.  
  3320. #if 0
  3321.   tree tags = CLASSTYPE_TAGS (type);
  3322.  
  3323.   while (tags)
  3324.     {
  3325.       tree code_type_node;
  3326.       tree tag;
  3327.  
  3328.       switch (TREE_CODE (TREE_VALUE (tags)))
  3329.     {
  3330.     case ENUMERAL_TYPE:
  3331.       code_type_node = enum_type_node;
  3332.       break;
  3333.     case RECORD_TYPE:
  3334.       code_type_node = record_type_node;
  3335.       break;
  3336.     case CLASS_TYPE:
  3337.       code_type_node = class_type_node;
  3338.       break;
  3339.     case UNION_TYPE:
  3340.       code_type_node = union_type_node;
  3341.       break;
  3342.     default:
  3343.       my_friendly_abort (297);
  3344.     }
  3345.       tag = xref_tag (code_type_node, TREE_PURPOSE (tags),
  3346.               TYPE_BINFO_BASETYPE (TREE_VALUE (tags), 0), 0);
  3347. #if 0 /* not yet, should get fixed properly later */
  3348.       pushdecl (make_type_decl (TREE_PURPOSE (tags), TREE_VALUE (tags)));
  3349. #else
  3350.       pushdecl (build_decl (TYPE_DECL, TREE_PURPOSE (tags), TREE_VALUE (tags)));
  3351. #endif
  3352.     }
  3353. #endif
  3354.  
  3355.   search_stack = push_search_level (search_stack, &search_obstack);
  3356.  
  3357.   id = TYPE_IDENTIFIER (type);
  3358. #if 0
  3359.   if (IDENTIFIER_TEMPLATE (id) != 0)
  3360.     {
  3361.       tree tmpl = IDENTIFIER_TEMPLATE (id);
  3362.       push_template_decls (DECL_ARGUMENTS (TREE_PURPOSE (tmpl)),
  3363.                TREE_VALUE (tmpl), 1);
  3364.       overload_template_name (id, 1);
  3365.     }
  3366. #endif
  3367.  
  3368.   /* Push class fields into CLASS_VALUE scope, and mark.  */
  3369.   dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarkedp);
  3370.  
  3371.   /* Compress fields which have only a single entry
  3372.      by a given name, and unmark.  */
  3373.   dfs_walk (TYPE_BINFO (type), dfs_compress_decls, markedp);
  3374.  
  3375.   /* Open up all the closed envelopes and push the contained decls into
  3376.      class scope.  */
  3377.   while (closed_envelopes)
  3378.     {
  3379.       tree new = TREE_PURPOSE (closed_envelopes);
  3380.       tree id;
  3381.  
  3382.       /* This is messy because the class value may be a *_DECL, or a
  3383.      TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
  3384.      *_DECLs.  The name is stored at different places in these three
  3385.      cases.  */
  3386.       if (TREE_CODE (new) == TREE_LIST)
  3387.     {
  3388.       if (TREE_PURPOSE (new) != NULL_TREE)
  3389.         id = TREE_PURPOSE (new);
  3390.       else
  3391.         {
  3392.           tree node = TREE_VALUE (new);
  3393.  
  3394.           while (TREE_CODE (node) == TREE_LIST)
  3395.         node = TREE_VALUE (node);
  3396.           id = DECL_NAME (node);
  3397.         }
  3398.     }
  3399.       else
  3400.     id = DECL_NAME (new);
  3401.  
  3402.       /* Install the original class value in order to make
  3403.      pushdecl_class_level work correctly.  */
  3404.       IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
  3405.       if (TREE_CODE (new) == TREE_LIST)
  3406.     push_class_level_binding (id, new);
  3407.       else
  3408.     pushdecl_class_level (new);
  3409.       closed_envelopes = TREE_CHAIN (closed_envelopes);
  3410.     }
  3411.   current_obstack = ambient_obstack;
  3412. }
  3413.  
  3414. /* Here's a subroutine we need because C lacks lambdas.  */
  3415. static void
  3416. dfs_unuse_fields (binfo)
  3417.      tree binfo;
  3418. {
  3419.   tree type = TREE_TYPE (binfo);
  3420.   tree fields;
  3421.  
  3422.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  3423.     {
  3424.       if (TREE_CODE (fields) != FIELD_DECL)
  3425.     continue;
  3426.  
  3427.       TREE_USED (fields) = 0;
  3428.       if (DECL_NAME (fields) == NULL_TREE
  3429.       && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
  3430.     unuse_fields (TREE_TYPE (fields));
  3431.     }
  3432. }
  3433.  
  3434. void
  3435. unuse_fields (type)
  3436.      tree type;
  3437. {
  3438.   dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
  3439. }
  3440.  
  3441. void
  3442. pop_class_decls (type)
  3443.      tree type;
  3444. {
  3445.   /* We haven't pushed a search level when dealing with cached classes,
  3446.      so we'd better not try to pop it.  */
  3447.   if (search_stack)
  3448.     search_stack = pop_search_level (search_stack);
  3449. }
  3450.  
  3451. void
  3452. print_search_statistics ()
  3453. {
  3454. #ifdef GATHER_STATISTICS
  3455.   if (flag_memoize_lookups)
  3456.     {
  3457.       fprintf (stderr, "%d memoized contexts saved\n",
  3458.            n_contexts_saved);
  3459.       fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
  3460.       fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
  3461.       fprintf (stderr, "fields statistics:\n");
  3462.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3463.            memoized_fast_finds[0], memoized_fast_rejects[0],
  3464.            memoized_fields_searched[0]);
  3465.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[0]);
  3466.       fprintf (stderr, "fnfields statistics:\n");
  3467.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3468.            memoized_fast_finds[1], memoized_fast_rejects[1],
  3469.            memoized_fields_searched[1]);
  3470.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[1]);
  3471.     }
  3472.   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
  3473.        n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
  3474.   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
  3475.        n_outer_fields_searched, n_calls_lookup_fnfields);
  3476.   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
  3477. #else
  3478.   fprintf (stderr, "no search statistics\n");
  3479. #endif
  3480. }
  3481.  
  3482. void
  3483. init_search_processing ()
  3484. {
  3485.   gcc_obstack_init (&search_obstack);
  3486.   gcc_obstack_init (&type_obstack);
  3487.   gcc_obstack_init (&type_obstack_entries);
  3488.  
  3489.   /* This gives us room to build our chains of basetypes,
  3490.      whether or not we decide to memoize them.  */
  3491.   type_stack = push_type_level (0, &type_obstack);
  3492.   _vptr_name = get_identifier ("_vptr");
  3493. }
  3494.  
  3495. void
  3496. reinit_search_statistics ()
  3497. {
  3498.   my_memoized_entry_counter = 0;
  3499.   memoized_fast_finds[0] = 0;
  3500.   memoized_fast_finds[1] = 0;
  3501.   memoized_adds[0] = 0;
  3502.   memoized_adds[1] = 0;
  3503.   memoized_fast_rejects[0] = 0;
  3504.   memoized_fast_rejects[1] = 0;
  3505.   memoized_fields_searched[0] = 0;
  3506.   memoized_fields_searched[1] = 0;
  3507.   n_fields_searched = 0;
  3508.   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
  3509.   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
  3510.   n_calls_get_base_type = 0;
  3511.   n_outer_fields_searched = 0;
  3512.   n_contexts_saved = 0;
  3513. }
  3514.  
  3515. static tree conversions;
  3516. static void
  3517. add_conversions (binfo)
  3518.      tree binfo;
  3519. {
  3520.   tree tmp = CLASSTYPE_FIRST_CONVERSION (BINFO_TYPE (binfo));
  3521.   for (; tmp && IDENTIFIER_TYPENAME_P (DECL_NAME (tmp));
  3522.        tmp = TREE_CHAIN (tmp))
  3523.     conversions = tree_cons (DECL_NAME (tmp), TREE_TYPE (TREE_TYPE (tmp)),
  3524.                  conversions);
  3525. }
  3526.  
  3527. tree
  3528. lookup_conversions (type)
  3529.      tree type;
  3530. {
  3531.   conversions = NULL_TREE;
  3532.   dfs_walk (TYPE_BINFO (type), add_conversions, 0);
  3533.   return conversions;
  3534. }
  3535.